Go's standard library has no database migration tool. It gives you database/sql to run SQL, embed to bundle migration files into the binary, and io/fs to read them, which is enough to write a small runner. For most projects it is better to use a maintained tool such as golang-migrate, goose or Atlas, and to run it from your pipeline or at startup.
A migration system does three things: it keeps ordered, versioned SQL files, records which versions have been applied in a table, and applies the pending ones safely.
| Need | Standard library | Typical addition |
|---|---|---|
| Run SQL, transactions | database/sql | A driver such as pgx (pgx/v5/stdlib), go-sql-driver/mysql or modernc.org/sqlite |
| Ship SQL files with the binary | embed, io/fs | None |
| Versioning, history table, locking, CLI | Not included | golang-migrate, goose, Atlas, dbmate |
| Tests | testing | A throwaway database, for example in a container |
database/sql is only an interface. You import a driver for its side effects, then call sql.Open, which does not connect until first use, so call PingContext to check the connection.
Use one file per change, a zero-padded version prefix so files sort correctly, and a descriptive name. Many tools expect separate up and down files.
Never edit a migration that has run in a shared environment. Add a new one instead, or the recorded history and the real schema drift apart.
This runner reads embedded .up.sql files in order, skips versions already recorded and applies each in its own transaction. It targets PostgreSQL, which supports transactional DDL. MySQL commits DDL implicitly, so a failed migration there can leave a partial change.
This is deliberately small. It has no down migrations and no locking. If two instances start together they can race, so a production runner should take a lock, for example a PostgreSQL advisory lock, before applying anything. That is a reason to use a tested tool instead of growing this one. Passing a multi-statement file to ExecContext works with the pgx driver but not with every driver, which is another place tools differ.
| Tool | Style | Notes |
|---|---|---|
| golang-migrate | Versioned up and down files, CLI and library | Many database drivers, can read from embedded files |
| goose | SQL or Go migrations, CLI and library | Supports migrations written in Go for data changes |
| Atlas | Declarative schema or versioned, with linting | Can plan changes by diffing the desired schema against the database |
| dbmate | Standalone CLI, plain SQL | Language independent |
| ORM migrations | Built into the ORM, for example GORM AutoMigrate | Convenient for prototypes, weaker for controlled production changes |
Check each project's current documentation before choosing, because features and supported drivers change. For a library such as golang-migrate you can load the embedded files through its iofs source and call Up() at startup or from a separate command.
NOT NULL constraint in a later release.CREATE INDEX CONCURRENTLY in PostgreSQL. That statement cannot run inside a transaction block, so it needs a migration that is not wrapped in one.No. database/sql runs queries, but versioning and history tracking come from tools such as golang-migrate, goose or Atlas.
It is fine for a single instance or small service, if you use a lock. For several replicas, run them as a separate job before the rollout.
Use //go:embed migrations/*.sql on an embed.FS variable. Most tools can read from an fs.FS, so no files need to exist on the server.
Not always. Many teams only write forward migrations and fix mistakes with a new migration, because a down migration often cannot restore deleted data.
Go gives you database/sql, embed and io/fs as building blocks, but not a migration framework. Keep ordered versioned SQL files, record applied versions in a table, apply each change in a transaction with a lock, and make schema changes backward compatible. For real projects use golang-migrate, goose or Atlas rather than maintaining your own runner.