{"id":377,"date":"2026-05-14T09:00:00","date_gmt":"2026-05-14T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/"},"modified":"2026-10-10T21:21:24","modified_gmt":"2026-10-10T15:51:24","slug":"go-database-migrations","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/","title":{"rendered":"Go Database Migrations with golang-migrate"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go database migrations keep your database schema in step with your code, one small numbered change at a time. In this post, you will learn what a migration is and how the popular golang-migrate tool tracks them. Also, you will write safe &#8220;up&#8221; and &#8220;down&#8221; files. Then we will run migrations from the command line and from Go code at startup.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the last lesson, <a href=\"https:\/\/smitabharat.com\/blog\/go-postgresql-pgx\/\">Go PostgreSQL with pgx<\/a>, we created the books table by hand in <code>psql<\/code>. Of course, that works on one laptop. However, it breaks down as soon as a team, a test server and production all need the same tables. You will also see <code>embed<\/code> and environment variables again, which we covered in <a href=\"https:\/\/smitabharat.com\/blog\/go-environment-variables-config\/\">Go environment variables for configuration<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Are Go Database Migrations?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In short, a migration is a small SQL file that changes the database schema in one step. For example, one migration creates a table, and the next one adds a column. Each migration has a version number, so the tool always knows which changes a database already has.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, think of migrations as a recipe written as numbered steps. So a new cook simply follows the steps from the top. Meanwhile, a cook who already did steps 1 to 3 continues from step 4. Similarly, Go database migrations bring any database, old or new, to the latest schema.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, every migration usually comes in a pair. So the &#8220;up&#8221; file applies the change, and the &#8220;down&#8221; file undoes it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Database Migrations Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Without migrations, however, schema changes live in someone&#8217;s head or in a chat message. As a result, environments drift apart and bugs appear only in production. Specifically, here is what migrations fix.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Same schema everywhere<\/strong>: laptops, CI, staging and production all run the same files.<\/li>\n\n\n\n<li><strong>History you can review<\/strong>: every schema change goes through a pull request, just like code.<\/li>\n\n\n\n<li><strong>Safe rollbacks<\/strong>: when a release goes wrong, a &#8220;down&#8221; file can undo the latest change.<\/li>\n\n\n\n<li><strong>Easy onboarding<\/strong>: a new developer runs one command, so the full schema appears in seconds.<\/li>\n\n\n\n<li><strong>Automatic deploys<\/strong>: the app or pipeline applies new migrations before new code runs.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In short, Go database migrations turn schema changes into normal, versioned code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Database Migrations Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First, you write numbered files in a folder, such as <code>000001_create_books_table.up.sql<\/code>. Then you run the tool against a database URL. Then the tool reads its own tracking table, <code>schema_migrations<\/code>, to find the current version. Finally, it applies every newer &#8220;up&#8221; file in order and saves the new version number.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-1-1024x640.png\" alt=\"Go Database Migrations diagram showing golang-migrate applying only pending files and updating schema_migrations\" class=\"wp-image-358\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">golang-migrate reads the current version from schema_migrations and applies only the newer up files, in order.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In particular, the tracking table has two columns. <code>version<\/code> holds the last applied number, and <code>dirty<\/code> is set to true while a migration runs. So if a migration fails halfway, the database stays marked as dirty until you fix it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Parts of golang-migrate<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In fact, golang-migrate works both as a command line tool and as a Go library. Also, it supports PostgreSQL, MySQL, SQLite and many more databases. So this table shows the commands you will use most.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>What it does<\/th><\/tr><\/thead><tbody><tr><td><code>migrate create -ext sql -dir db\/migrations -seq NAME<\/code><\/td><td>Creates a new numbered up and down file<\/td><\/tr><tr><td><code>migrate -path db\/migrations -database URL up<\/code><\/td><td>Applies all pending migrations<\/td><\/tr><tr><td><code>migrate ... down 1<\/code><\/td><td>Undoes only the latest migration<\/td><\/tr><tr><td><code>migrate ... version<\/code><\/td><td>Prints the current version<\/td><\/tr><tr><td><code>migrate ... force N<\/code><\/td><td>Sets the version after a manual fix<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">File Naming<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Generally, each file name has a version, a description, a direction and the <code>.sql<\/code> extension. For instance, <code>000002_add_stock_to_books.up.sql<\/code>. The <code>-seq<\/code> flag gives simple numbers like <code>000001<\/code>, while the default uses a timestamp. Simple numbers are easier to read, so we use them here. However, busy teams often prefer timestamps, because two people rarely pick the same one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Up and Down Files<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An &#8220;up&#8221; file moves the schema forward, while its &#8220;down&#8221; file reverses exactly that change. So if <code>up<\/code> adds a column, <code>down<\/code> drops the same column.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Database URL<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, a PostgreSQL URL looks like <code>postgres:\/\/user:pass@localhost:5432\/shop?sslmode=disable<\/code>. Therefore, keep it in an environment variable instead of typing passwords into commands.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Migrations vs Manual Changes vs Auto-Migrate<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Overall, there are three common ways to change a schema. So the table below compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>SQL migrations<\/th><th>Manual SQL in psql<\/th><th>ORM auto-migrate<\/th><\/tr><\/thead><tbody><tr><td>Versioned history<\/td><td>Yes<\/td><td>No<\/td><td>Partly<\/td><\/tr><tr><td>Can roll back<\/td><td>Yes, with down files<\/td><td>Only by hand<\/td><td>Rarely<\/td><\/tr><tr><td>Reviewed in pull requests<\/td><td>Yes<\/td><td>No<\/td><td>Only as Go structs<\/td><\/tr><tr><td>Handles data changes<\/td><td>Yes, any SQL<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Same result everywhere<\/td><td>Yes<\/td><td>No<\/td><td>Mostly<\/td><\/tr><tr><td>Good for<\/td><td>Real projects<\/td><td>Quick experiments<\/td><td>Small demos<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-2-1024x640.png\" alt=\"Go Database Migrations diagram showing up and down steps between schema versions\" class=\"wp-image-359\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Each migration is a pair of files: up moves the schema forward one step, and down moves it back by exactly that step.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So when should you use which? Use SQL migrations for any project that has more than one database. On the other hand, manual SQL is fine for a quick local test you will throw away.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hands-On: Go Database Migrations for a Book Store<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s write Go database migrations for the book store from the pgx lesson. First we create a table, then add a column and finally add an index, each as its own migration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Install the CLI and Create Files<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, install the tool with Go. Here, the build tag adds PostgreSQL support.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go install -tags 'postgres' github.com\/golang-migrate\/migrate\/v4\/cmd\/migrate@latest\nmigrate create -ext sql -dir db\/migrations -seq create_books_table\nmigrate create -ext sql -dir db\/migrations -seq add_stock_to_books\nmigrate create -ext sql -dir db\/migrations -seq add_author_index<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then each <code>create<\/code> command makes two empty files. As a result, the folder now holds six files numbered 000001 to 000003.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Write the SQL<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now fill in each pair. For clarity, here are all six files, one after another.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- 000001_create_books_table.up.sql\nCREATE TABLE IF NOT EXISTS books (\n    id     BIGSERIAL PRIMARY KEY,\n    title  TEXT NOT NULL,\n    author TEXT NOT NULL\n);\n\n-- 000001_create_books_table.down.sql\nDROP TABLE IF EXISTS books;\n\n-- 000002_add_stock_to_books.up.sql\nALTER TABLE books ADD COLUMN stock INTEGER NOT NULL DEFAULT 0;\n\n-- 000002_add_stock_to_books.down.sql\nALTER TABLE books DROP COLUMN IF EXISTS stock;\n\n-- 000003_add_author_index.up.sql\nCREATE INDEX IF NOT EXISTS idx_books_author ON books (author);\n\n-- 000003_add_author_index.down.sql\nDROP INDEX IF EXISTS idx_books_author;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Notice the <code>DEFAULT 0<\/code> in migration 2. Without it, adding a <code>NOT NULL<\/code> column would fail on a table that already has rows. Also, <code>IF EXISTS<\/code> and <code>IF NOT EXISTS<\/code> make each file safe to run again. We ran all three up files and then all three down files against PostgreSQL 16. As a result, the schema went forward and back cleanly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Run the Migrations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, apply all Go database migrations and check the version.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export DATABASE_URL=\"postgres:\/\/postgres:secret@localhost:5432\/shop?sslmode=disable\"\nmigrate -path db\/migrations -database \"$DATABASE_URL\" up\nmigrate -path db\/migrations -database \"$DATABASE_URL\" version\nmigrate -path db\/migrations -database \"$DATABASE_URL\" down 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>up<\/code> command prints one line per applied migration, and <code>version<\/code> then prints <code>3<\/code>. Then, after <code>down 1<\/code>, the index is gone and the version goes back to <code>2<\/code>. The output looks like this, although your timings will differ.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\/u create_books_table (8.1ms)\n2\/u add_stock_to_books (12.4ms)\n3\/u add_author_index (16.9ms)\n3\n3\/d add_author_index (5.2ms)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Run Go Database Migrations at Startup<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Many teams run Go database migrations from the app itself when it starts. So the files ship inside the binary with <code>embed<\/code>, and no extra tool is needed in production.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"embed\"\n\t\"errors\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com\/golang-migrate\/migrate\/v4\"\n\t_ \"github.com\/golang-migrate\/migrate\/v4\/database\/postgres\"\n\t\"github.com\/golang-migrate\/migrate\/v4\/source\/iofs\"\n)\n\n\/\/go:embed migrations\/*.sql\nvar migrationsFS embed.FS\n\nfunc runMigrations(dbURL string) error {\n\tsrc, err := iofs.New(migrationsFS, \"migrations\")\n\tif err != nil {\n\t\treturn err\n\t}\n\tm, err := migrate.NewWithSourceInstance(\"iofs\", src, dbURL)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer m.Close()\n\tif err := m.Up(); err != nil &amp;&amp; !errors.Is(err, migrate.ErrNoChange) {\n\t\treturn err\n\t}\n\treturn nil\n}\n\nfunc main() {\n\tif err := runMigrations(os.Getenv(\"DATABASE_URL\")); err != nil {\n\t\tlog.Fatalf(\"migrate: %v\", err)\n\t}\n\tlog.Println(\"database is up to date\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s walk through it, since this pattern appears in many Go services. First of all, the <code>\/\/go:embed<\/code> line copies every <code>.sql<\/code> file into the binary at build time. Then <code>iofs.New<\/code> turns that embedded folder into a migration source. Finally, <code>m.Up()<\/code> applies pending files, and <code>migrate.ErrNoChange<\/code> simply means the database was already current, so we treat it as success.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Fixing a Dirty Database<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes a migration fails. For example, suppose migration 2 has a typo and fails halfway. Then every later command stops with <code>error: Dirty database version 2. Fix and force version.<\/code> To recover, first fix the database by hand so it matches version 1. Next, run <code>migrate ... force 1<\/code> to clear the dirty flag. Finally, correct the SQL file and run <code>up<\/code> again.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Go Database Migrations<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Editing a migration that already ran<\/strong>: other databases never see the change. Instead, always add a new migration.<\/li>\n\n\n\n<li><strong>Adding NOT NULL without a default<\/strong>: the migration fails on tables that have rows. So add a default or fill the column first.<\/li>\n\n\n\n<li><strong>Writing a down file that does not match<\/strong>: rollbacks then break the schema. Instead, make each down undo exactly its up.<\/li>\n\n\n\n<li><strong>Two developers using the same number<\/strong>: one file is skipped or conflicts. Use timestamps on busy teams, or check before merging.<\/li>\n\n\n\n<li><strong>Running migrations from many app copies at once<\/strong>: they may race. However, golang-migrate takes a database lock, so let it handle this, or run migrations in a separate deploy step.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Database Migrations<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Keep each migration small<\/strong>, because small steps are easy to review and undo.<\/li>\n\n\n\n<li><strong>Test up, then down, then up again in CI<\/strong>, so broken down files are caught early.<\/li>\n\n\n\n<li><strong>Never put secrets in migration files<\/strong>, since they live in Git forever.<\/li>\n\n\n\n<li><strong>Separate schema and big data changes<\/strong>, because long data updates can lock tables.<\/li>\n\n\n\n<li><strong>Embed migrations in the binary<\/strong>, so the app and its schema always ship together.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To practise Go database migrations, add a migration that creates a <code>sales<\/code> table. Give it <code>id<\/code>, <code>book_id<\/code> and <code>sold_at TIMESTAMPTZ NOT NULL DEFAULT now()<\/code>. Then write the matching down file and run <code>up<\/code>, <code>down 1<\/code> and <code>up<\/code> again.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hint: add a foreign key with <code>book_id BIGINT NOT NULL REFERENCES books(id)<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These pages cover the tool, its CLI and Go&#8217;s embed feature, so check them for every option.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/golang-migrate\/migrate\">golang-migrate on GitHub<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/github.com\/golang-migrate\/migrate\/v4\">migrate package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/embed\">Go embed package documentation<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go database migrations are numbered SQL files that change the schema step by step.<\/li>\n\n\n\n<li>Also, each migration has an up file and a matching down file.<\/li>\n\n\n\n<li>golang-migrate tracks the current version in the <code>schema_migrations<\/code> table.<\/li>\n\n\n\n<li>Then use <code>up<\/code>, <code>down 1<\/code>, <code>version<\/code> and <code>force<\/code> from the CLI.<\/li>\n\n\n\n<li>Embed migrations and run <code>m.Up()<\/code> at startup, ignoring <code>ErrNoChange<\/code>.<\/li>\n\n\n\n<li>Finally, never edit a migration that already ran; add a new one instead.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now our schema is versioned. Next, we will compare two popular ways to write the queries themselves. Specifically, we look at generating code with sqlc and using the GORM library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next lesson: <a href=\"https:\/\/smitabharat.com\/blog\/go-sqlc-vs-gorm\/\">Go sqlc vs GORM: Which Should You Use?<\/a><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Go database migrations keep your database schema in step with your code, one small numbered change at a time. In this post, you will&hellip;<\/p>\n","protected":false},"author":1,"featured_media":357,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[15],"tags":[11,71,23,21],"swapz_series":[],"class_list":["post-377","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-lang","tag-go-lang","tag-go-migrations","tag-go-tutorial","tag-golang-basics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Database Migrations with golang-migrate<\/title>\n<meta name=\"description\" content=\"Learn Go database migrations with golang-migrate: write up and down SQL files, run them from the CLI and Go, and fix dirty versions safely.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-database-migrations\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Database Migrations with golang-migrate\" \/>\n<meta property=\"og:description\" content=\"Learn Go database migrations with golang-migrate: write up and down SQL files, run them from the CLI and Go, and fix dirty versions safely.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-database-migrations\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-05-14T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-10T15:51:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Database Migrations with golang-migrate\",\"datePublished\":\"2026-05-14T03:30:00+00:00\",\"dateModified\":\"2026-10-10T15:51:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/\"},\"wordCount\":1489,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-database-migrations-featured.png\",\"keywords\":[\"Go Lang\",\"Go Migrations\",\"Go Tutorial\",\"Golang Basics\"],\"articleSection\":[\"Go Lang\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/\",\"name\":\"Go Database Migrations with golang-migrate\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-database-migrations-featured.png\",\"datePublished\":\"2026-05-14T03:30:00+00:00\",\"dateModified\":\"2026-10-10T15:51:24+00:00\",\"description\":\"Learn Go database migrations with golang-migrate: write up and down SQL files, run them from the CLI and Go, and fix dirty versions safely.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-database-migrations-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-database-migrations-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Database Migrations featured image: cards for up files, down files and version tracking\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-database-migrations\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Database Migrations with golang-migrate\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Database Migrations with golang-migrate","description":"Learn Go database migrations with golang-migrate: write up and down SQL files, run them from the CLI and Go, and fix dirty versions safely.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/","og_locale":"en_US","og_type":"article","og_title":"Go Database Migrations with golang-migrate","og_description":"Learn Go database migrations with golang-migrate: write up and down SQL files, run them from the CLI and Go, and fix dirty versions safely.","og_url":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-05-14T03:30:00+00:00","article_modified_time":"2026-10-10T15:51:24+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Database Migrations with golang-migrate","datePublished":"2026-05-14T03:30:00+00:00","dateModified":"2026-10-10T15:51:24+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/"},"wordCount":1489,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-featured.png","keywords":["Go Lang","Go Migrations","Go Tutorial","Golang Basics"],"articleSection":["Go Lang"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/","url":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/","name":"Go Database Migrations with golang-migrate","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-featured.png","datePublished":"2026-05-14T03:30:00+00:00","dateModified":"2026-10-10T15:51:24+00:00","description":"Learn Go database migrations with golang-migrate: write up and down SQL files, run them from the CLI and Go, and fix dirty versions safely.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-database-migrations\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-database-migrations-featured.png","width":2400,"height":1260,"caption":"Go Database Migrations featured image: cards for up files, down files and version tracking"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-database-migrations\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Database Migrations with golang-migrate"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=377"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/377\/revisions"}],"predecessor-version":[{"id":387,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/377\/revisions\/387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/357"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=377"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}