What's the correct way to copy entire database (its structure and data) to a new one in pgAdmin?
|
Postgres allows the use of any existing database on the server as a template when creating a new database. I'm not sure whether pgAdmin gives you the option on the create database dialog but you should be able to execute the following in a query window if it doesn't:
Still, you may get:
|
|||||||||||||||||||||
|
A command-line version of Bell's answer:
This should be run under the privileges of the database master, usually postgres. |
|||||||||
|
To clone an existing database with postgres you can do that
IT will kill all the connection to the source db avoiding the error
|
|||||||||
|
Don't know about pgAdmin, but pgdump gives you a dump of the database in SQL. You only need to create a database by the same name and do 'psql mydatabase < mydump' to restore all of the tables and their data and all access privileges. |
|||||||||||||
|
In production environment, where the original database is under traffic, I'm simply using:
|
|||||||||
|
I pieced this approach together with the examples from above. I'm working on an "under load" server and got the error when I attempted the approach from @zbyszek. I also was after a "command line only" solution.
Here's what worked for me (Commands prepended with
|
||||
|
In pgAdmin you can make a backup from your original database, and then just create a new database and restore from the backup just created. Regards |
|||||
|
What's the correct way to copy entire database (its structure and data) to a new one in pgAdmin? Answer:
Tried and tested. |
|||||||||
|
PostgreSQL 9.1.2:
|
|||||||||
|
First,
Go to PostgreSQL command line:
Create the new database, give the rights and exit:
Copy structure and data from the old database to the new one:
|
|||||||||
|
For those still interested, I have come up with a bash script that does (more or less) what the author wanted. I had to make a daily business database copy on a production system, this script seems to do the trick. Remember to change the database name/user/pw values.
|
|||
|
If the database has open connections, this script may help. I use this to create a test database from a backup of the live-production database every night. This assumes that you have an .SQL backup file from the production db (I do this within webmin).
|
|||
|
To create database dump
To resote database dump
|
|||
|
Using pgAdmin, disconnect the database that you want to use as a template. Then you select it as the template to create the new database, this avoids getting the already in use error. |
|||
|
Try this:
gl XD |
||||
|