
PostgreSQL is a powerful open-source database system. It is known for its reliability and advanced features. Sometimes, you may need to copy a database. You may want to create a backup. You may want to test your app in a different environment. Or maybe you are moving to a new server. This article will show you five easy methods to perform a Postgres Copy Database task. Each method is simple and uses clear steps.
Method 1: Using pg_dump
and pg_restore
This is the most common method. It involves dumping the source database and restoring it to a new one.
Step-by-step:
- Dump the source database
bashCopyEditpg_dump -U postgres -Fc source_db > source_db.dump
This command creates a binary backup file.-Fc
means the format is custom.
- Create a new target database
bashCopyEditcreatedb -U postgres target_db
- Restore the dump into the new database
bashCopyEditpg_restore -U postgres -d target_db source_db.dump
This copies all data, tables, and schema.
Pros:
- Good for backups
- Supports large databases
Cons:
- Takes time with big datasets
Use this method when copying between servers or keeping backups.
Method 2: Using createdb
with the --template
Option
This method works when copying a database within the same PostgreSQL server. It uses the source database as a template.
Step-by-step:
bashCopyEditcreatedb -U postgres new_db --template=old_db
This copies the entire old_db
into new_db
.
The new database will be the same.
Pros:
- Fast
- Easy one-line command
Cons:
- Only works on the same server
- Source database must be idle (no active connections)
Use this when working locally or during maintenance hours.
Method 3: Using psql
with SQL Commands
You can use SQL queries inside psql
to export and import data.
This method uses pg_dump
but in plain SQL format.
Step-by-step:
- Dump the source database with plain SQL
bashCopyEditpg_dump -U postgres -Fp source_db > source_db.sql
-Fp
means the format is plain SQL.
- Create a new database
bashCopyEditcreatedb -U postgres target_db
- Import the SQL file into the new database
bashCopyEditpsql -U postgres -d target_db -f source_db.sql
Pros:
- Human-readable SQL dump
- Easy to edit the SQL file
Cons:
- Slower than custom format
- No compression
Use this method when you want to see or change the structure before restoring.
Method 4: Using pgAdmin
(Graphical Tool)
If you prefer a GUI over the terminal, this method is for you. pgAdmin is a free tool for managing PostgreSQL visually.
Step-by-step:
- Open pgAdmin and connect to your server
- Right-click on the source database > Backup
- Choose format: Custom or Plain
- Click Backup to create the dump file
- Create a new database in pgAdmin
- Right-click the new database > Restore
- Choose the dump file and click Restore
Pros:
- Beginner-friendly
- No command line needed
Cons:
- Slower than CLI
- Not ideal for automation
Use pgAdmin when working on a local machine or for learning purposes.
Method 5: Copying Database Objects and Data Manually
This method involves creating the schema and then copying tables. It is helpful when you want to copy only part of the database.
Step-by-step:
- Connect using
psql
bashCopyEditpsql -U postgres -d source_db
- Generate schema dump
bashCopyEditpg_dump -s -U postgres source_db > schema.sql
- Create a target database
bashCopyEditcreatedb -U postgres target_db
- Import schema
bashCopyEditpsql -U postgres -d target_db -f schema.sql
- Copy tables individually
bashCopyEditINSERT INTO target_db.public.table1 SELECT * FROM source_db.public.table1;
Repeat for each table.
Pros:
- Fine control over data
- Ideal for partial copy
Cons:
- Time-consuming
- Risk of missing tables or data
Use this when copying only selected tables or objects.
Tips for Using Postgres Copy Database Safely
- Always back up before copying
Mistakes can lead to data loss. - Check the database size before using SQL dump
Large databases may take longer. - Make sure no active connections
Some methods need the source database to be idle. - Use correct user permissions
The user must have the right to read and write databases. - Use
pg_stat_activity
to check active connections
sqlCopyEditSELECT * FROM pg_stat_activity;
This helps avoid conflicts during the copy.
Summary of Methods
Method | Best For | Tools Needed |
---|---|---|
pg_dump + pg_restore | Full backup and restore | Command Line |
createdb --template | Fast local copy | Command Line |
pg_dump (plain) + psql | SQL visibility and editing | Command Line |
pgAdmin GUI | Beginners and GUI preference | pgAdmin |
Manual copy of schema and tables | Partial data copy | Command Line + SQL |
Final Thoughts
There are many ways to perform a Postgres Copy Database task. Each method fits a different use case.
- Use
pg_dump
for full backups and server-to-server copies. - Use
--template
for fast copies on the same server. - Use
pgAdmin
for simple visual copying. - Use manual copy when dealing with selected tables.
Choose the one that fits your needs best. With these methods, you can easily manage your PostgreSQL databases.