Merging local PostgreSQL database file to AWS RDS database

纵然是瞬间 提交于 2021-01-07 03:39:55

问题


I have a local Postgres database in my local machine. Now I would like to merge a local Postgres database file to AWS existing RDS database. Does anyone know how to do this? Thank you in advance.


回答1:


If the RDS instance is in a private subnet, then you need to tunnel through an EC2 instance to get to your RDS instance. Assuming your security groups are set so that you can ssh into the EC2 instance, and the EC2 instance has access to RDS on port 5432, you can do the following:

  1. Make a dump of your local database:
$ pg_dump -Fc --no-acl --no-owner -h localhost -U<username> <database_name> -f <filename>

where <username> is the Postgres username on the local computer ('postgres' is the default user). For example:

$ pg_dump -Fc --no-acl --no-owner -h localhost -Upostgres my_development_db -f data.dump
  1. On your local computer, set up a tunnel into the RDS instance. This example assumes that ec2-user is the default user on your EC2 instance, which is the case if you are using an AWS image.
$ ssh -i /path/to/ssh/key -fNL 5433:[database_host]:5432 ec2-user@[app_host]

For example:

$ ssh -i ~/.ssh/ida_rsa -fNL 5433:my_prod_db.cbaxyzxyz.us-west-1.rds.amazonaws.com:5432 ec2-user@ec2-11-222-333-4.us-west-1.compute.amazonaws.com
  1. Then, still on your local computer, import the local database dump into the remote RDS database server:
$ pg_restore --no-owner -n public -c -1 -p 5433 -U<username> -h 127.0.0.1 -d <database_name> <filename>

For example:

$ pg_restore --no-owner -n public -c -1 -p 5433 -Umy_prod_username -h 127.0.0.1 -d prod_db_name data.dump

Enter the RDS database password when prompted.

Note that the -c ("clean") option, will drop the database objects before recreating them from your local database dump.



来源:https://stackoverflow.com/questions/55810078/merging-local-postgresql-database-file-to-aws-rds-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!