0

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?

4
  • 1
    Possible duplicate of How to generate create script of table using SQL query in SQL Server Commented Oct 13, 2016 at 22:21
  • @johan855: Not a duplicate. Postgres is not SQL Server . Commented Oct 14, 2016 at 5:57
  • 1
    You are approaching this from the wrong end. Your production database should have been created from SQL scripts stored in a version control system - including all schema changes. If you need to setup a test database, run those scripts from the version control system. Tools like Liquibase or Flyway will help you organize and track the execution of those scripts Commented Oct 14, 2016 at 5:58
  • @horse i sort of disagree - the truth ultimate lies in the DB itself not in VCS, there is no need to put SQL in VCS, that might be overkill? Commented Oct 14, 2016 at 8:25

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.