How to run SQL scripts and get data on application startup?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Running SQL scripts at application startup is a common requirement for initializing database schemas, seeding reference data, and applying migrations. Different frameworks provide built-in mechanisms for this, from Spring Boot's auto-execution of data.sql files to Node.js migration libraries. This article covers the main approaches across popular frameworks.
Spring Boot
Automatic Script Execution
Spring Boot automatically executes schema.sql and data.sql files found in src/main/resources/:
Configure in application.properties:
Using Flyway (Recommended for Production)
Flyway provides versioned, repeatable migrations:
Place migration scripts in src/main/resources/db/migration/:
Using @PostConstruct or CommandLineRunner
For programmatic initialization:
Node.js / Express
Using Knex.js Migrations
Run migrations programmatically at startup:
Using Sequelize
Django
Django uses its built-in migration system:
For data seeding, use fixtures or data migrations:
.NET / Entity Framework
Getting Data at Startup
To load reference data into memory at startup:
Spring Boot
Node.js
Common Pitfalls
- Execution order: Schema scripts must run before data scripts. In Spring Boot,
schema.sqlruns beforedata.sqlby default, but Flyway/Liquibase handle ordering via version numbers. - Idempotency: Startup scripts run every time the application starts. Use
IF NOT EXISTS,ON CONFLICT DO NOTHING, or check-before-insert to avoid duplicate data or errors on restart. - Microservices Architecture: Each microservice should only manage its own database schema. Avoid cross-service SQL scripts.
- Test Environments: Use separate seed scripts for tests to reset and repopulate database state before integration tests. Consider using transactions that roll back after each test.
- Production vs Development: Use
spring.sql.init.mode=embedded(default) for development with H2, andspring.sql.init.mode=neverfor production where Flyway handles migrations.
Summary
- Spring Boot auto-executes
schema.sqlanddata.sqlfrom resources; use Flyway for production migrations - Node.js: use Knex or Sequelize migrations run programmatically before
app.listen() - Django: use the built-in migration system with
RunPythonfor data seeding - .NET: call
db.Database.Migrate()inProgram.csat startup - Always make startup scripts idempotent to handle restarts safely
Related reading
- How to sample large database and implement K-means and K-nn in R?
- How to save Amazon Redshift output to local CSV through SQL Workbench?
- How to save enum in database as string
- How to save MySQL query output to excel or .txt file?
- How to schedule a stored procedure in MySQL
- how to search for a given word from a huge database?
- How to search JSON data in MySQL?
- How to see full query from SHOW PROCESSLIST?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.