Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Best Practices for Using pg_dump

Updated on 2024-09-30 GMT+08:00

Description

pg_dump is a native tool for backing up a PostgreSQL database. The file created by pg_dump can be a SQL script file or an archive file. For details, see pg_dump.

  • SQL script file: It is a plain-text file that contains the SQL commands required to rebuild a database to the state when it was backed up.
  • Archive file: It must be used with pg_restore to rebuild a database. This format allows pg_restore to select the data to be restored.

Precautions

pg_dump dumps a single database, schemas, or tables. Only tables, data, and functions can be exported. Before restoration, you need to create a database and account in the target instance in advance.

  • --format=custom: The dump file is in binary format, which can be used only by pg_restore. You can restore specific tables from a dump file.
  • --format=plain: The dump file is in plain-text format. To restore from such a plain-text file, connect to the database and execute the file.

Constraints

Before using pg_dump and pg_restore, ensure that the versions of the source and target databases are the same to avoid compatibility issues. Incompatible versions may cause data loss or restoration errors.

Preparing Test Data

# Create a database.
create database dump_database;

# Log in to the database.
\c dump_database

# Create table 1 and insert data into the table.
create table dump_table(id int primary key, content char(50));
insert into dump_table values(1,'aa');
insert into dump_table values(2,'bb');

# Create table 2 and insert data into the table.
create table dump_table2(id int primary key, content char(50));
insert into dump_table2 values(1,'aaaa');
insert into dump_table2 values(2,'bbbb');

Using pg_dump to Export a Database to a SQL File

Syntax

pg_dump --username=<DB_USER> --host=<DB_IPADDRESS> --port=<DB_PORT> --format=plain --file=<BACKUP_FILE><DB_NAME>
  • DB_USER indicates the database username.
  • DB_IPADDRESS indicates the database address.
  • DB_PORT indicates the database port.
  • BACKUP_FILE indicates the name of the file to be exported.
  • DB_NAME indicates the name of the database to be exported.
  • --format indicates the format of the exported file. plain (default) indicates a plain-text file that contains SQL scripts. For details about other options, see pg_dump.

Examples

  • Export a database to a SQL file (INSERT statements).
     $ pg_dump --username=root --host=192.168.61.143 --port=5432 --format=plain --inserts --file=backup.sql dump_database
      Password for user root:
  • Export all table schemas from a database to a SQL file.
     $ pg_dump --username=root --host=192.168.61.143 --port=5432 --format=plain --schema-only --file=backup.sql dump_database
      Password for user root:
  • Export all table data from a database to a SQL file.
      $ pg_dump --username=root --host=192.168.61.143 --port=5432 --format=plain --data-only --file=backup.sql dump_database
      Password for user root:

After the commands in any of the above examples are executed, a backup.sql file will be generated as follows:

[rds@localhost ~]$ ll backup.sql
-rw-r----- 1 rds rds 5657 May 24 09:21 backup.sql

Using pg_dump to Export Specified Tables from a Database to a SQL File

Syntax

pg_dump --username=<DB_USER> --host=<DB_ADDRESS> --port=<DB_PORT> --format=plain --file=<BACKUP_FILE> <DB_NAME> --table=<TABLE_NAME>
  • DB_USER indicates the database username.
  • DB_ADDRESS indicates the database address.
  • DB_PORT indicates the database port.
  • BACKUP_FILE indicates the name of the file to be exported.
  • DB_NAME indicates the name of the database to be migrated.
  • TABLE_NAME indicates the name of the specified table in the database to be migrated.
  • --format indicates the format of the exported file. plain (default) indicates a plain-text file that contains SQL scripts. For details about other options, see pg_dump.

Examples

  • Export a single table from a database to a SQL file.
      $ pg_dump --username=root --host=192.168.61.143 --port=5432 --format=plain --file=backup.sql dump_database --table=dump_table
      Password for user root
  • Export multiple tables from a database to a SQL file.
     $ pg_dump --username=root --host=192.168.61.143 --port=5432 --format=plain --file=backup.sql dump_database --table=dump_table --table=dump_table2
      Password for user root:
  • Export all tables starting with ts_ from a database to a SQL file.
      $ pg_dump --username=root --host=192.168.61.143 --port=5432 --format=plain --file=backup.sql dump_database --table=ts_*
      Password for user root:
  • Export all tables excluding those starting with ts_ from a database to a SQL file.
     $ pg_dump --username=root --host=192.168.61.143 --port=5432 --format=plain --file=backup.sql dump_database -T=ts_*
      Password for user root:

After the commands in any of the above examples are executed, a backup.sql file will be generated as follows:

[rds@localhost ~]$ ll backup.sql
-rw-r----- 1 rds rds 5657 May 24 09:21 backup.sql

Using pg_dump to Export Data of a Specific Schema

Syntax

pg_dump --username=<DB_USER> --host=<DB_IPADDRESS> --port=<DB_PORT> --format=plain --schema=<SCHEMA> <DB_NAME> --table=<BACKUP_FILE>
  • DB_USER indicates the database username.
  • DB_IPADDRESS indicates the database address.
  • DB_PORT indicates the database port.
  • BACKUP_FILE indicates the name of the file to be exported.
  • DB_NAME indicates the name of the database to be exported.
  • SCHEMA indicates the name of the schema to be exported.
  • --format indicates the format of the exported file. plain (default) indicates a plain-text file that contains SQL scripts. For details about other options, see pg_dump.

Examples

  • Export all data of the public schema from a database.
    pg_dump --username=root --host=192.168.61.143 --port=5432 --format=plain --schema=public  dump_database  > backup.sql
  • Export all data except the public schema from a database in a customized compression format.
    pg_dump --username=root --host=192.168.61.143 --port=5432 --format=custom -b -v -N public dump_database > all_sch_except_pub.backup

Restoring Data

To restore from a plain-text SQL script file, run the psql command. See the following example commands:

# Restore a specific database.
psql --username=root --host=192.168.61.143 --port=5432  backup_database  < backup.sql

# Restore a specific table.
psql --username=root --host=192.168.61.143 --port=5432  backup_database --table=dump_table  < backup.sql

# Restore a specific schema.
psql --username=root --host=192.168.61.143 --port=5432  backup_database --schema=public  < backup.sql
NOTE:

Before the restoration, create a database named backup_database in the target database.

To restore from other file formats, use pg_restore. pg_restore is used to restore a PostgreSQL database in any non-plain-text format dumped by pg_dump.

pg_restore --username=root --host=192.168.61.143 --port=5432 --dbname=backup_database --format=custom all_sch_except_pub.backup --verbose

FAQ

  1. What should I do if an error about insufficient permissions is reported for pg_dump?

    Solution:

    Check whether the root user is used to export data. If any other user account is used, an error about insufficient permissions will be reported. If the root user is used and an error is still reported, check the database version. You can run pg_dump commands as the root user only when the kernel version support root privilege escalation. For details about the kernel versions that support root privilege escalation, see Privileges of the root User.

  2. Why an error was reported for functions such as control_extension after I imported a dump file to the target RDS for PostgreSQL database?

    Solution:

    That's because the target database contains these functions. This error can be ignored.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback