Question

I have two databases on the same server. One named A and one named B. Booth databases have the same structure. I want to empty database B and load it with data from database A. Which is the best way to do this?

I have tried to take backup of database A in plain format. Then open the resulting sql-file and replace every occurence of 'A' with 'B' and then run the sql-script. This worked but I think it should be an easier way to move data from one database to another. Is it?

I use 'pgAdmin III' as my tool, but this is not necessary.

This is my first post here, hope the question is relevant and structured well enough. I tried google first but found it hard to find anyone with the same question.

Thanks in advance! /David

SOLUTION: After help from Craig, this is how I did it

pg_dump -Fc -a -f a.dbbackup A

psql -c 'TRUNCATE table1, table2, ..., tableX CASCADE'

pg_restore dblive.backup -d B -c (not sure if -c was necessary)
Was it helpful?

Solution

Backup:

pg_dump -Fc -f a.dbbackup

Restore:

psql -c 'CREATE DATABASE b;'
pg_restore --dbname b a.dbbackup

Use the -U, -h etc options as required to connect to the correct host as the correct user with permissions to dump, create and restore the DB. See the docs for psql, pg_dump and pg_restore for more info (they all take the same options for connection control).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top