Migrations for PostgreSQL
PLpgSQL Shell
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Stampede is a PostgreSQL database migration utility.


  • Write migrations in SQL: no special language needed.
  • Atomic execution: on failure or cancel, the entire transaction is rolled back.
  • Unapply migrations: optionally, if you add unapply statements to your migrations, you can tell Stampede to unapply migrations.


Set the PGDATABASE, PGHOST, PGPORT and PGUSER environment variables, then execute src/stampede migrate. You'll probably want to wrap this in a shell script or a Makefile.

Stampede will look for migrations in ./migrations/, in files ending with .sql. Example:

select stampede.define_migration(
    id := 42,
    name := 'Add customer_id foreign key'
    apply := '
        alter table orders
        add constraint orders_customer_fk
        foreign key (customer_id) references customers (id)
    unapply := 'drop constraint orders_customer_fk'

apply and unapply can contain any number of statements. unapply and name are optional.

Migrations are executed in ascending order of id. Each migration must have a unique id.


  • Show usage: stampede
  • List migrations and status: stampede status
  • Apply all unapplied migrations: stampede migrate
  • Apply the first unapplied migration: stampede step
  • Unapply the last applied migration: stampede back

The following commands are destructive, but very useful when writing and testing your migrations. Don't use these on a production database.

  • Unapply all applied migrations: stampede unapply
  • Unapply all applied migrations, then apply all migrations: stampede stomp
  • Unapply last applied migration, then apply all migrations: stampede redo
  • Drop schema public: stampede drop
  • Drop schema public, then apply all migrations: stampede reset
  • Clone target database, then apply all migrations: stampede clone targetdb