PostgreSQL supports logical backups. You can use the pg_dump logical backup function to export backup files and then import them to RDS using psql.
You can access RDS instances through an EIP or from an ECS over a private network.
For details, see 5.
The PostgreSQL client version must be the same as the DB engine version of your RDS for PostgreSQL instance. A PostgreSQL database or client will provide pg_dump and psql.
Before migrating an existing PostgreSQL database to RDS, you need to export data first.
pg_dump --username=<DB_USER> --host=<DB_ADDRESS> --port=<DB_PORT> --format=plain --file=<BACKUP_FILE><DB_NAME>
Enter the database password as prompted.
If the exported SQL file uses INSERT statements, you can easily edit and modify the file. However, the speed of importing data may be slower than that of using COPY statements. You are advised to select a right statement format as needed.
For more information, see pg_dump options.
Examples:
$ pg_dump --username=root --host=192.168.151.18 --port=5432 --format=plain --file=backup.sql my_db
Password for user root:
$ pg_dump --username=root --host=192.168.151.18 --port=5432 --format=plain --inserts --file=backup.sql my_db
Password for user root:
$ pg_dump --username=root --host=192.168.151.18 --port=5432 --format=plain --schema-only --file=backup.sql my_db
Password for user root:
$ pg_dump --username=root --host=192.168.151.18 --port=5432 --format=plain --data-only --file=backup.sql my_db
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 2714 Sep 21 08:23 backup.sql
pg_dump --username=<DB_USER> --host=<DB_ADDRESS> --port=<DB_PORT> --format=plain --file=<BACKUP_FILE> <DB_NAME> --table=<TABLE_NAME>
Enter the database password as prompted.
Examples:
$ pg_dump --username=root --host=192.168.151.18 --port=5432 --format=plain --file=backup.sql my_db --table=test
Password for user root:
$ pg_dump --username=root --host=192.168.151.18 --port=5432 --format=plain --file=backup.sql my_db --table=test1 --table=test2
Password for user root:
$ pg_dump --username=root --host=192.168.151.18 --port=5432 --format=plain --file=backup.sql my_db --table=ts_*
Password for user root:
$ pg_dump --username=root --host=192.168.151.18 --port=5432 --format=plain --file=backup.sql my_db -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 2714 Sep 21 08:23 backup.sql
If the destination database does not exist, run the following command to create a database:
# psql --host=<RDS_ADDRESS> --port=<DB_PORT> --username=root --dbname=postgres -c "create database<DB_NAME>;"
# psql --host=<RDS_ADDRESS> --port=<DB_PORT> --username=root --dbname=<DB_NAME> --file=<BACKUP_DIR>/backup.sql
Enter the password for the RDS DB instance when prompted.
Example:
# psql --host=172.16.66.198 --port=5432 --username=root --dbname=my_db --file=backup.sql
Password for user root:
my_db=> \l my_db
In this example, the database named my_db has been imported.
my_db=> \l my_dbList of databasesName | Owner | Encoding | Collate | Ctype | Access privileges------+-------+----------+-------------+-------------+-----------my_db | root | UTF8 | en_US.UTF-8 | en_US.UTF-8 |(1 row)