简体   繁体   English

将本地PostgreSQL数据库文件合并到AWS RDS数据库

[英]Merging local PostgreSQL database file to AWS RDS database

I have a local Postgres database in my local machine. 我在本地机器上有一个本地Postgres数据库。 Now I would like to merge a local Postgres database file to AWS existing RDS database. 现在我想将本地Postgres数据库文件合并到AWS现有的RDS数据库。 Does anyone know how to do this? 有谁知道如何做到这一点? Thank you in advance. 先感谢您。

If the RDS instance is in a private subnet, then you need to tunnel through an EC2 instance to get to your RDS instance. 如果RDS实例位于私有子网中,则需要通过EC2实例隧道到达RDS实例。 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: 假设您的安全组已设置为可以进入EC2实例,并且EC2实例可以访问端口5432上的RDS,您可以执行以下操作:

  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). 其中<username>是本地计算机上的Postgres用户名('postgres'是默认用户)。 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. 在本地计算机上,设置到RDS实例的隧道。 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. 此示例假定ec2-user是EC2实例上的默认用户,如果您使用的是AWS映像,则属于这种情况。
$ 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: 然后,仍然在本地计算机上,将本地数据库转储导入远程RDS数据库服务器:
$ 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. 出现提示时输入RDS数据库密码。

Note that the -c ("clean") option, will drop the database objects before recreating them from your local database dump. 请注意, -c (“clean”)选项将在从本地数据库转储重新创建数据库对象之前删除它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM