postgresqlschema

Create a dev database with same table structure as production in Postgres 9.3


I want to create a 'development' database for my web application.

I'm using Postgres 9.3, and I would like 'devdb' to have the exact table structure as my production 'appdb'. I do not want them to share data, but I want devdb to receive any changes made to table structures, if this is possible. (ie. if i add a new table in appdb, I want devdb to also have the new table, same thing if I remove a column)

Do I need to use schemas for this, and if so, how? My appdb currently has a schema of public.

Thanks!


Solution

  • I think your best bet is to use:

    pg_dump --schema-only prod | psql dev
    

    To keep the schemas in sync, either drop and reload the dev db, or script your schema changes so you can apply the change to both DBs. You should be doing that anyway, testing changes in dev before applying them to production.

    (Tools like Liquibase can be interesting for this).

    Attempts to link DDL definitions directly are unsafe. They create a dependency from production to dev. That's risky.

    For example, if you were to use a table inheritance based approach then a long-running transaction holding a lock on the dev tables might cause delays on production.