Open In App

PostgreSQL – Copy Database

Last Updated : 30 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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:

  1. Testing and Development: Creating a duplicate environment.
  2. Database Backup: Ensuring data safety through redundancy.
  3. Database Migration: Transferring databases from one server to another.

Let’s go through two main approaches:

  1. Copying a PostgreSQL database within the same server.
  2. Copying a PostgreSQL database to a different server.

Copying a PostgreSQL Database Within the Same Server

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 PostgreSQL Database from One Server to Another

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 Database

Step 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.sql

Example: 

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 thedvdrental’ 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 dvdrental

Conclusion

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.

FAQs

Does PostgreSQL have loops?

Yes, PostgreSQL supports loops in PL/pgSQL with LOOP, WHILE, FOR, and FOREACH statements, which can be used to perform repetitive tasks within functions or DO blocks.

Are there while loops in SQL?

SQL itself doesn’t directly support loops, but many databases, including PostgreSQL, support looping constructs within procedural languages like PL/pgSQL, enabling WHILE and other loops in functions and scripts

How does a while () loop work?

A while loop runs a block of code repeatedly as long as a specified condition remains true. In each iteration, it evaluates the condition: if true, it executes the code within the loop; if false, it exits the loop.



Next Article

Similar Reads

three90RightbarBannerImg