PostgreSQL - Copy Database
Last Updated :
30 Oct, 2024
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:
- Testing and Development: Creating a duplicate environment.
- Database Backup: Ensuring data safety through redundancy.
- Database Migration: Transferring databases from one server to another.
Let's go through two main approaches:
- Copying a PostgreSQL database within the same server.
- 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 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 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.
Similar Reads
PostgreSQL Tutorial In this PostgreSQL tutorial youâll learn the basic data types(Boolean, char, text, time, int etc.), Querying and Filtering techniques like select, where, in, order by, etc. managing and modifying the tables in PostgreSQL. Weâll cover all the basic to advance concepts of PostgreSQL in this tutorial.
8 min read
PostgreSQL DATEDIFF Function PostgreSQL doesnât have a DATEDIFF function like some other databases, but you can still calculate the difference between dates using simple subtraction. This approach allows you to find out how many days, months, or years separate two dates. In this article, we'll explore how to compute date differ
6 min read
PostgreSQL - Data Types PostgreSQL is a powerful, open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column. which allows developers to define, store, and manipulate data in a way that aligns w
5 min read
PostgreSQL - Psql commands PostgreSQL, or Postgres, is an object-relational database management system that utilizes the SQL language. PSQL is a powerful interactive terminal for working with the PostgreSQL database. It enables users to execute queries efficiently and manage databases effectively.Here, we highlight some of th
2 min read
Top 50 PostgreSQL Interview Questions and Answers Are you preparing for a PostgreSQL interview? PostgreSQL is a powerful open-source relational database management system (RDBMS) that is well-known for its reliability, scalability, and rich set of features. Itâs a favorite among developers and businesses alike, making it essential to master if we w
15+ min read
PostgreSQL - Create Database Creating a database in PostgreSQL is an important task for developers and database administrators to manage data effectively. PostgreSQL provides multiple ways to create a database, catering to different user preferences, whether through the command-line interface or using a graphical interface like
5 min read
How to Dump and Restore PostgreSQL Database? PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases betw
6 min read
PostgreSQL - SERIAL When working with PostgreSQL, we need to create tables with unique primary keys. PostgreSQL offers a powerful feature known as the SERIAL pseudo-type which simplifies generating auto-incrementing sequences for columns. In this article, weâll learn about the PostgreSQL SERIAL pseudo-type by explain h
5 min read
PostgreSQL - DISTINCT ON expression The DISTINCT ON clause in PostgreSQL allows us to retrieve unique rows based on specific columns by offering more flexibility than the standard DISTINCT clause. DISTINCT ON allow us to specify which row to keep for each unique value based on an ORDER BY clause. This is particularly useful for select
5 min read
PostgreSQL Connection String A connection string is an essential component that enables applications to communicate with databases or other data sources by providing the necessary configuration details. It consolidates critical information such as the server address, database name, user credentials, and additional parameters li
4 min read