Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to sync two PostgreSQL databases (some tables from development db to production db) sometimes.

So I came up with this script:

pg_dump -a -F tar -t table1 -t table2 -U user1 dbname1 | \
pg_restore -a -U user2 -d dbname2

The problem is that this works just for newly added rows. When I edit non-PK column I get constraint error and row isn't updated. For each dumped row I need to check if it exists in destination database (by PK) and if so delete it before INSERT/COPY.

Thanks for your advice.

(Previously posted on, but IMHO this is better place for this question).

share|improve this question

In any environment I've seen, this is typically not done on a table by table basis.

  • There are a number of possibilities for any row: SELECT / INSERT / UPDATE / ALTER ... you would need to take care of all of these possibilities.

  • Tables are not the only things that live inside databases, especially PostgreSQL databases. Sequences, functions, aggregates, views etc. can possibly all need to be copied over.

If I were in your situation, I'd dump the whole database and use it to restore a clean version.

pg_dump --clean --no-owner --no-privileges dbname ## other pg_dump / psql options
share|improve this answer
That's not possible in my case, because I have some tables that must be preserved in development and production DB (customers, ...). – woky Nov 27 '09 at 10:29

Maybe using dblink and inserting only what you need from a select?
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.