Automatically Run SQL Scripts on Startup with yugabyted

When you’re spinning up a new YugabyteDB instance, it’s often useful to pre-load your database with schema definitions, seed data, or custom functions. With the yugabyted CLI, you can automate this using the --initial_scripts_dir flag.

The --initial_scripts_dir flag lets you specify a directory containing SQL scripts that should be executed automatically when a new database is initialized. This is a great way to:

  1. Set up your schema

  2. Pre-populate tables with test data

  3. Define stored procedures or functions

Just point yugabyted start to your directory of .sql files, then all .sql files in the directory will be executed in alphanumeric order during cluster startup.

Example:

I have created the following .sqlscripts:

				
					[root@cloud-server-0 test]# pwd
/root/test

[root@cloud-server-0 test]# ll
total 16
-rw-r--r--. 1 root root 19 May 30 21:40 a_create_database.sql
-rw-r--r--. 1 root root 29 May 30 21:41 b_create_schema.sql
-rw-r--r--. 1 root root 53 May 30 21:41 c_create_table.sql
-rw-r--r--. 1 root root 62 May 30 21:41 d_insert_data.sql

[root@cloud-server-0 test]# tail -n +1 *
==> a_create_database.sql <==
CREATE TABLE test;

==> b_create_schema.sql <==
\c test;
CREATE SCHEMA test;

==> c_create_table.sql <==
\c test;
CREATE TABLE test.test(c1 INT PRIMARY KEY);

==> d_insert_data.sql <==
\c test;
INSERT INTO test.test SELECT generate_series(1, 10);
				
			

I’ll spin up a new cluster with the .sql scripts executed as part of the startup process:

				
					[root@cloud-server-0 test]# yugabyted start --base_dir=~/yb01 --initial_scripts_dir=/root/test > ~/logs/start.log

[root@cloud-server-0 test]# ysqlsh -h $(hostname -I) -d test -c "SELECT * FROM test.test ORDER BY 1;"
 c1
----
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
(10 rows)
				
			
At startup, the test database, schema, and table were automatically created as expected!

Scripts are run only when a new database is initialized – not on every restart. This makes it perfect for local development, CI pipelines, or containerized environments where repeatable setup is key.

Have Fun!

My wife and I spent the week camping in Acadia National Park at the Blackwoods Campground. The scenery was stunning, but it rained almost the entire time. One night, I was sure the tent would leak and flood—but it held up like a champ! Hard to believe we bought it at K-Mart 18 years ago.