postgresql-simple-migration: PostgreSQL Schema Migrations

[ bsd3, database, library, program ] [ Propose Tags ]

A PostgreSQL-simple schema migration utility

[Skip to Readme]




Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Versions [RSS],,,,,,,,,,,,,,,
Dependencies base (>=4.6 && <5.0), base64-bytestring (>=1.0 && <1.1), bytestring (>=0.10 && <0.11), cryptohash (>=0.11 && <0.12), directory (>=1.2 && <1.3), postgresql-simple (>=0.4 && <0.5) [details]
License BSD-3-Clause
Copyright 2014, Andreas Meingast
Author Andreas Meingast <>
Maintainer Andreas Meingast <>
Category Database
Home page
Bug tracker
Source repo head: git clone git://
Uploaded by ameingast at 2014-07-05T05:10:56Z
Distributions NixOS:
Reverse Dependencies 1 direct, 0 indirect [details]
Executables migrate
Downloads 11416 total (60 in the last 30 days)
Rating 1.75 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Successful builds reported [all 1 reports]

Readme for postgresql-simple-migration-

[back to package description]

PostgreSQL Migrations for Haskell

Build Status

Welcome to postgresql-simple-migrations, a tool for helping you with PostgreSQL schema migrations.

This project is an open-source database migration tool. It favors simplicity over configuration.

It is implemented in Haskell and uses the (excellent) postgresql-simple library to communicate with PostgreSQL.

It comes in two flavors: a library that features an easy to use Haskell API and as a standalone application.

Database migrations can be written in SQL (in this case PostgreSQL-sql) or in Haskell.


Database migrations should not be hard. They should be under version control and documented in both your production systems and in your project files.


This library executes SQL/Haskell migration scripts and keeps track of their meta information.

Scripts are be executed exactly once and any changes to scripts will cause a run-time error notifying you of a corrupted database.

The meta information consists of:

  • an MD5 checksum of the executed script to make sure already existing scripts cannot be modified in your production system.
  • a time-stamp of the date of execution so you can easily track when a change happened.


This utility can be used in two ways: embedded in your Haskell program or as a standalone binary.


The standalone program supports file-based migrations. To execute all SQL-files in a directory $BASE_DIR, execute the following command to initialize the database in a first step.

CON="host=$host dbname=$db user=$user password=$pw"
./dist/build/migrate/migrate init $CON
./dist/build/migrate/migrate migrate $CON $BASE_DIR

For more information about the PostgreSQL connection string, see: libpq-connect.


The library supports more actions than the standalone program.

Initializing the database:

main :: IO ()
main = do
    let url = "host=$host dbname=$db user=$user password=$pw"
    con <- connectPostgreSQL (BS8.pack url)
    void $ runMigration $ MigrationContext MigrationInitialization True con

For file-based migrations, the following snippet can be used:

main :: IO ()
main = do
    let url = "host=$host dbname=$db user=$user password=$pw"
    let dir = "."
    con <- connectPostgreSQL (BS8.pack url)
    void $ runMigration $ MigrationContext (MigrationDirectory dir) True con

To run Haskell-based migrations, use this:

main :: IO ()
main = do
    let url = "host=$host dbname=$db user=$user password=$pw"
    let name = "my script"
    let script = "create table users (email varchar not null)";
    con <- connectPostgreSQL (BS8.pack url)
    void $ runMigration $ MigrationContext (MigrationScript name script) True con

Compilation and Tests

The program is built with the cabal build system. The following command builds the library, the standalone binary and the test package.

cabal configure --enable-tests && cabal build -j

To execute the tests, you need a running PostgreSQL server with an empty database called test. Tests are executed through cabal as follows:

cabal configure --enable-tests && cabal test

To Do

  • Collect executed scripts and check if already executed scripts have been deleted.
  • Validate command for the standalone program.