Logical backup can write data stored in the database to a file while PostgreSQL is running. This means that you do not have to stop the database to perform backup. Since backup data is used for recovery, logical backup can only recover to the point when the backup was performed.
There are the following three commands to perform logical backup.
The table below summarizes the 3 types of logical backup
Overview | Backup command | Backup target | Recovery command | Restore point | ||
Backup | Latest | Any | ||||
Data is retrieved and saved in SQL format while the database is running. It is also possible to back up definition and data. |
pg_dumpall | Database cluster | psql | Yes | No | No |
pg_dump | Database | psql (script file) |
Yes | No | No | |
pg_restore (archive file format) |
||||||
COPY | Table | COPY | Yes | No | No |
Remember that you can select the backup method depending on the data range you need - the entire database cluster, a specific database, or a table. Also, you can specify the contents of the backup with options, such as just the table definition, only the data in the table, or both the table definition and the data.
Compared to physical backup, the backup data size is smaller, but the processing time is longer. Since this method only uses backup data, you can only restore to the time when backup is performed.
As an example, we assume an employee information system built with a PostgreSQL database to explain the backup and recovery of the PostgreSQL database.
OS | Red Hat® Enterprise Linux® 7 (for Intel64) |
PostgreSQL version | 10.3 |
Instance name | inst1 |
Database name | mydb |
Table name | Employee table 1 (jtbl1) |
Based on this specification, we will go through a sample operation.
The following is an example of using the pg_dumpall command to back up the entire database cluster and then using the psql command for recovery.
The current instance (inst1) of server A needs to be migrated to a new instance (inst2) on the same server.
The image below illustrates this example.
Follow the steps below to achieve the above.
The following is an example of backing up a specific table using the pg_dump command and recovering using the psql command.
In this example, let's assume that the department code (code) of the employee table 1 (jtbl1) needs to be changed due to an organizational change.
To address the requirements like above, you can follow the steps below.
Caution
If you specify the -c or --clean option when executing the pg_dump command, the SQL command that deletes database objects will also be output before the SQL command that creates database objects. Carefully consider if this option is necessary.
Take a backup using the SQL command COPY. This command outputs the data stored in a table to a file.
Next, we will create employee table 2 (jtbl2) with the same structure as employee table 1 (jtbl1).
To address the requirements like above, you can follow the steps below.
In some cases, when data is copied from a table to a file with COPY, the resulting data may not be encoded with the expected character code. This is because copied data is encoded according to the ENCODING option or the current client encoding. If the ENCODING parameter of COPY is omitted, then the client encoding is adopted, which may result in a different character code. To make sure the correct encoding is used, specify the desired encoding in the ENCODING parameter of COPY.
Leverage your business data with a peace of mind by performing the logical backup according to the requirements of your business.
Fujitsu Enterprise Postgres is designed for developers, data architects, and DBAs seeking the efficiency of a leading open source based database technology with the enhanced security and performance features plus support required in a serious business environment.
Read our most recent articles regarding all aspects of PostgreSQL and Fujitsu Enterprise Postgres.