Alexander Mills
Alexander Mills

Reputation: 100010

Query a postgres database for the SQL which generates the tables

We want to replicate our local test DB automatically, given the structure of the production DB, on the fly / on demand.

What would be nice, would be to query the prod DB and retrieve the SQL that generates the tables/views and then run that SQL against a cleaned out local DB.

Or perhaps there is a better way to replicate a production DB on a local machine?

What is the best way to do this?

Upvotes: 0

Views: 103

Answers (1)

Iurii Tkachenko
Iurii Tkachenko

Reputation: 3189

If I understood your question correct, you need to copy DB schema. You can use pg_dump with --schema-only parameter to do this. It'll only dump schema of the database, and you can import it locally.

$ pg_dump mydb --schema-only > mydb-schema.sql

Upvotes: 2

Related Questions