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

编程入门 行业动态 更新时间:2024-10-28 10:25:57
本文介绍了将本地PostgreSQL数据库文件合并到AWS RDS数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的本​​地计算机上有一个本地Postgres数据库.现在,我想将本地Postgres数据库文件合并到AWS现有RDS数据库.有谁知道如何做到这一点?预先谢谢你.

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.

推荐答案

如果RDS实例位于专用子网中,则需要通过EC2实例进行隧道传输才能到达RDS实例.假设已设置安全组,以便可以通过SSH进入EC2实例,并且EC2实例可以访问端口5432上的RDS,则可以执行以下操作:

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:

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

    其中< username> 是本地计算机上的Postgres用户名("postgres"是默认用户).例如:

    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

  • 在本地计算机上,建立到RDS实例的隧道.此示例假定 ec2-user 是EC2实例上的默认用户,如果您使用的是AWS映像,则是这种情况.
  • 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]

    例如:

    $ ssh -i ~/.ssh/ida_rsa -fNL 5433:my_prod_db.cbaxyzxyz.us-west-1.rds.amazonaws:5432 ec2-user@ec2-11-222-333-4.us-west-1pute.amazonaws

  • 然后,仍在本地计算机上,将本地数据库转储导入到远程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数据库密码.

    Enter the RDS database password when prompted.

    请注意, -c (干净")选项将删除数据库对象,然后再从本地数据库转储中重新创建它们.

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

    更多推荐

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

    本文发布于:2023-10-17 12:44:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1500934.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数据库文件   数据库   PostgreSQL   RDS   AWS

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!