snippetsqlCritical
How to duplicate the database in postgresql?
Viewed 0 times
postgresqltheduplicatedatabasehow
Problem
I need to duplicate the existing database including its schema and structure to another new database. I need this in shell command environment and not in pgadmin. Please kindly help me.
my user is "postgres"
nohup pg_dump exampledb > example-01.sql
createdb -O postgres exampledbclone_01my user is "postgres"
nohup psql exampledbclone_01 < example-01.sql$ pg_dump mydb > db.sql
$ psql -d newdb -f db.sqlSolution
If you want to duplicate it within the same PostgreSQL install and you don't have actively connected users there's a handy shortcut:
or from the shell
Otherwise you'll need to use
If you're using
CREATE DATABASE my_new_database TEMPLATE my_old_database;or from the shell
createdb -T my_old_database my_new_database;Otherwise you'll need to use
pg_dump, createdb and pg_restore, e.g.pg_dump -Fc -f olddb.pgdump -d olddb &&\
createdb newdb &&\
pg_restore -d newdb olddb.pgdumpIf you're using
nohup so the command doesn't die if you lose your ssh session, consider using screen instead.Code Snippets
CREATE DATABASE my_new_database TEMPLATE my_old_database;createdb -T my_old_database my_new_database;pg_dump -Fc -f olddb.pgdump -d olddb &&\
createdb newdb &&\
pg_restore -d newdb olddb.pgdumpContext
StackExchange Database Administrators Q#137636, answer score: 61
Revisions (0)
No revisions yet.