2

How can I dump my database schema and data in such a way that the usernames, database names and the schema names of the dumped data matches these variables on the servers I deploy to?

My current process entails moving the data in two steps. First, I dump the schema of the database (pg_dump --schema-only -C -c) then I dump out the data with pg_dump --data-only -C and restore these on the remote server in tandem using the psql command. But there has to be a better way than this.

3
  • What version of PostgreSQL? Commented Jul 5, 2014 at 19:12
  • Have you considered replication instead dump/restore? Commented Jul 5, 2014 at 19:13
  • @DwayneTowell I've never done replication. I thought replication was used for multi-server setups. I have just one server for now. Can you give me some direction for where to find resources on this topic? Commented Jul 5, 2014 at 19:17

1 Answer 1

4

We use the following to replicate databases.

pg_basebackup -x -P -D /var/lib/pgsql/9.2/data -h OTHER_DB_IP_ADDR -U postgres

It requires the "master" server at OTHER_DB_IP_ADDR to be running the replication service and pg_hba.conf must allow replication connections. You do not have to run the "slave" service as a hot/warm stand by in order to replicate. One downside of this method compared with a dump/restore, the restore operation effectively vacuums and re-indexes and resets EVERYTHING, while the replication doesn't, so replicating can use a bit more disk space if your database has been heavily edited. On the other hand, replicating is MUCH faster (15 mins vs 3 hours in our case) since indexes do not have to be rebuilt.

Some useful references:

http://opensourcedbms.com/dbms/setup-replication-with-postgres-9-2-on-centos-6redhat-el6fedora/

http://www.postgresql.org/docs/9.2/static/high-availability.html

http://www.rassoc.com/gregr/weblog/2013/02/16/zero-to-postgresql-streaming-replication-in-10-mins/

Sign up to request clarification or add additional context in comments.

1 Comment

Just want to add, that it is possible (at least now in v11) to pass the entire conninfo string as one command line argument: pg_basebackup -d 'host=127.0.0.1 port=25432' -D /postgres/data/path

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.