Goose: A Database Migration Tool for Go
Daniel Hayes
Full-Stack Engineer · Leapcell

Key Takeaways
- Goose simplifies database migrations with support for SQL and Go-based migrations.
- It supports multiple databases and allows embedding migrations in Go binaries.
- Provides essential migration commands like
up
,down
,status
, andversion
.
Goose is a database migration tool designed to help manage your database schema by creating incremental SQL changes or Go functions. It offers both a command-line interface (CLI) and a library, making it versatile for various use cases.
Features
-
Multi-Database Support: Goose works with various databases, including Postgres, MySQL, SQLite, YDB, ClickHouse, MSSQL, Vertica, and more.
-
Go Migrations: Supports migrations written as plain Go functions, allowing for complex migrations that can't be expressed in SQL alone.
-
Embedded Migrations: Allows embedding migrations within your Go binaries, facilitating distribution and deployment.
-
Out-of-Order Migrations: Supports applying migrations that were missed or applied out of sequence.
-
Seeding Data: Enables seeding of initial or test data into the database.
-
Environment Variable Substitution: Allows the use of environment variables within SQL migrations for dynamic configurations.
Installation
To install the Goose CLI, run:
go install github.com/pressly/goose/v3/cmd/goose@latest
This command installs the goose
binary to your $GOPATH/bin
directory.
For macOS users, Goose is also available as a Homebrew formula:
brew install goose
If you require a smaller binary, you can build a lite version by excluding unnecessary drivers:
go build -tags='no_postgres no_mysql no_sqlite3 no_ydb' -o goose ./cmd/goose
Available build tags include: no_clickhouse
, no_libsql
, no_mssql
, no_mysql
, no_postgres
, no_sqlite3
, no_vertica
, and no_ydb
.
Usage
Goose provides several commands to manage your database schema:
-
Create a New Migration:
To create a new SQL migration:
goose create AddSomeColumns sql
This command generates a new SQL migration file with a timestamped prefix.
To create a new Go migration:
goose create AddSomeColumns go
This command generates a new Go migration file with the appropriate boilerplate code.
-
Apply Migrations:
To migrate the database to the most recent version available:
goose up
This command applies all available migrations.
-
Rollback Migrations:
To roll back a single migration from the current version:
goose down
This command rolls back the most recently applied migration.
-
Migration Status:
To print the status of all migrations:
goose status
This command displays which migrations have been applied and which are pending.
-
Current Version:
To print the current version of the database:
goose version
This command outputs the current migration version of the database.
Writing Migrations
Goose supports migrations written in SQL or Go.
-
SQL Migrations:
A sample SQL migration looks like:
-- +goose Up CREATE TABLE post ( id INT NOT NULL, title TEXT, body TEXT, PRIMARY KEY(id) ); -- +goose Down DROP TABLE post;
The annotations -- +goose Up
and -- +goose Down
denote the statements to be executed for migrating up and down, respectively.
-
Go Migrations:
A sample Go migration looks like:
package main import ( "database/sql" "fmt" ) func Up_20130106222315(txn *sql.Tx) { fmt.Println("Hello from migration 20130106222315 Up!") } func Down_20130106222315(txn *sql.Tx) { fmt.Println("Hello from migration 20130106222315 Down!") }
The functions Up_20130106222315
and Down_20130106222315
define the behavior of the migration for applying and rolling back, respectively.
Conclusion
Goose is a powerful and flexible tool for managing database migrations in Go applications. Its support for multiple databases, both SQL and Go migrations, and features like embedded migrations and out-of-order migrations make it a valuable asset for developers looking to maintain and evolve their database schemas effectively.
For more detailed information and advanced configurations, refer to the Goose GitHub repository and the official documentation.
FAQs
Goose works with Postgres, MySQL, SQLite, MSSQL, ClickHouse, YDB, and more.
Use the command goose create <MigrationName> sql
to generate a migration file.
Yes, Goose supports migrations written in Go functions for complex changes.
We are Leapcell, your top choice for hosting Go projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ