Last Updated : 15 Jul, 2025
Copying a PostgreSQL database is essential for tasks like testing, backup, and database migration. PostgreSQL offers efficient tools for duplicating databases either within the same server or across different servers, ensuring data consistency and integrity.
This guide explains step-by-step methods for copying a PostgreSQL database on the same server and transferring it to a remote server for seamless database backup and migration. Whether you are setting up a development environment or creating redundancy, PostgreSQL simplifies the process for database administrators and developers alike.
Why Copy a PostgreSQL Database?Database copying is helpful for:
Let's go through two main approaches:
Sometimes, we may need to create a copy of a PostgreSQL database within the same server, often for testing or development purposes. PostgreSQL makes this simple with the CREATE DATABASE
statement using the TEMPLATE
option.
Syntax:
CREATE DATABASE target_database
WITH TEMPLATE source_database;
Example
This statement copies the 'source_database' to the 'target_database'. For instance, to copy the dvdrental sample database which is described here and can be downloaded from here, to the 'dvdrental_test database', we use the following statement:
Query:
CREATE DATABASE dvdrental_test
WITH TEMPLATE dvdrental;
This command will create a copy of dvdrental
as dvdrental_test
.It will take a while to complete copying which depends upon the size of the original database.
Output
After running the above command, we can check if the new database was successfully created by listing all databases in PostgreSQL:
\l
You should see dvdrental_test
listed among the databases, confirming that the database copy was successful.
Copying a database between different PostgreSQL servers can be done in several ways. The connection speed between servers can affect the time required, especially for larger databases. One common method is to create a database dump and restore it on the target server.
Steps to Copy a DatabaseStep 1: Create a Dump file of the source database.
pg_dump -U postgres -d source_database -f source_database.sql
Step 2: Copy the dump file to the remote server.
Step 3: Create a new database in the remote server where you want to restore the database dump:
CREATE DATABASE target_database;
Step 4: Restore the dump file on the remote server:
psql -U postgres -d target_database -f source_database.sqlExample:
Here we will copy the 'dvdrental' database from the local server to the remote server.
Step 1: First, we will dump the 'dvdrental' database into a dump file e.g., 'dvdrental.sql':
pg_dump -U postgres -O dvdrental dvdrental.sql
Step 2: Then we will copy the dump file to a remote server and we will create the 'dvdrental' database on the remote server:
CREATE DATABASE dvdrental;
Step 3: Now, we will restore the dump file that we just created into the remote server:
psql -U postgres -d dvdrental -f dvdrental.sql
Step 4: For high-speed connections between servers or for smaller databases, you can also use the following command:
pg_dump -C -h local -U localuser source_database | psql -h remote -U remoteuser target_database
Step 5: For instance, if one desires to copy the 'dvdrental' database from the localhost to the remote server, you do it as follows:
pg_dump -C -h localhost -U postgres dvdrental | psql -h remote -U postgres dvdrentalConclusion
Copying a PostgreSQL database within the same server or across servers is a common yet essential task for database administrators and developers. Using these methods, you can efficiently create backup copies, set up testing environments, or perform database migrations. PostgreSQL provides flexible and robust tools to achieve these tasks, ensuring smooth data handling and transfer.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4