【问题标题】:Merging local PostgreSQL database file to AWS RDS database将本地 PostgreSQL 数据库文件合并到 AWS RDS 数据库
【发布时间】:2019-09-12 13:43:26
【问题描述】:

我的本​​地机器上有一个本地 Postgres 数据库。现在我想将本地 Postgres 数据库文件合并到 AWS 现有的 RDS 数据库。有谁知道如何做到这一点?提前谢谢你。

【问题讨论】:

  • 您的 RDS 实例是在公有子网还是私有子网中?
  • RDS 实例位于私有子网中。我正在使用命令恢复 Postgres 数据库 pg_restore -v -h xxx -U master -d test /home/xxx/Documents/xxx/DB/back.backup
  • 在连接到 rds 时出现错误“服务器是否在主机“xxx”(xxx) 上运行,接受端口 5432 上的 TCP/IP 连接?”

标签: postgresql ubuntu-14.04 amazon-elastic-beanstalk rds


【解决方案1】:

如果 RDS 实例位于私有子网中,则您需要通过 EC2 实例建立隧道才能访问您的 RDS 实例。假设您的安全组已设置为可以 ssh 进入 EC2 实例,并且 EC2 实例可以访问端口 5432 上的 RDS,您可以执行以下操作:

  1. 转储本地数据库:
$ pg_dump -Fc --no-acl --no-owner -h localhost -U<username> <database_name> -f <filename>

其中&lt;username&gt; 是本地计算机上的 Postgres 用户名('postgres' 是默认用户)。例如:

$ pg_dump -Fc --no-acl --no-owner -h localhost -Upostgres my_development_db -f data.dump
  1. 在您的本地计算机上,设置一条通往 RDS 实例的隧道。此示例假定 ec2-user 是您的 EC2 实例上的默认用户,如果您使用的是 AWS 映像就是这种情况。
$ ssh -i /path/to/ssh/key -fNL 5433:[database_host]:5432 ec2-user@[app_host]

例如:

$ 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. 然后,仍然在您的本地计算机上,将本地数据库转储导入远程 RDS 数据库服务器:
$ pg_restore --no-owner -n public -c -1 -p 5433 -U<username> -h 127.0.0.1 -d <database_name> <filename>

例如:

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

出现提示时输入 RDS 数据库密码。

请注意,-c(“clean”)选项将在从本地数据库转储中重新创建数据库对象之前删除它们。

【讨论】:

  • 我正在使用 AWS 弹性豆茎。我无法使用以下命令ssh -i ~/.ssh/xyz.pub -fNL 5433:xxx.rds.amazonaws.com:5432 ec2-user@xxx.elasticbeanstalk.com 建立隧道
  • 我不确定这里的用户名是什么。这是 rds 数据库用户名还是 aws elastic beanstalk 用户名?
  • 此外,我还通过 eb ssh --setup 命令并使用公钥 ~/.ssh/xyz.pub 为 aws elastic beanstalk 环境创建了 ssh。不确定我在这里做错了什么。
  • debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * debug2: resolving "xxx.elasticbeanstalk.com" port 22 debug2: ssh_connect_direct: needpriv 0 debug1: Connecting to xxx.central-1.elasticbeanstalk.com [xxx] port 22. debug1: connect to address xxx port 22: Connection refused debug1: Connecting to xxx.elasticbeanstalk.com [xxx] port 22. debug1: connect to address xxx port 22: Connection refused ssh: connect to host xxx.elasticbeanstalk.com port 22: Connection refused
  • 我可以使用 eb ssh 环境名称连接
猜你喜欢
  • 1970-01-01
  • 2021-05-17
  • 2016-12-05
  • 2022-10-25
  • 2015-11-16
  • 2019-12-24
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
相关资源
最近更新 更多