【问题标题】:Heroku Postgres connection from another ec2 instance来自另一个 ec2 实例的 Heroku Postgres 连接
【发布时间】:2025-12-18 17:20:02
【问题描述】:

如何从另一个运行在不同云服务上的 web 应用程序连接到 heroku postgres。

我有一个 heroku postgres。我在 EC2 上运行了另一个 Web 应用程序。当我在 ec2 上的 webapp 尝试连接到 heroku 应用时,它失败了。

我尝试在端口 5432 上从 ec2 实例远程登录到 heroku postgres,但失败了。

谁能指点一下?

【问题讨论】:

    标签: heroku


    【解决方案1】:

    您可以使用heroku pg:credentials 命令获取数据库的凭据。例如:

    $ heroku pg:credentials DATABASE_URL 
    Connection info string:
       "dbname=abcdefghijklmn host=ec2-1-2-3-4.compute-1.amazonaws.com port=5432 user=abcdefghijklmn password=abcdefghijklmnopqrstuvwxyz sslmode=require"
    Connection URL:
        postgres://abcdefghijklmn:abcdefghijklmnopqrstuvwxyz@ec2-1-2-3-4.compute-1.amazonaws.com:5432/abcdefghijklmn
    

    然后,您可以在其他应用程序中使用连接 URL 或分解的凭据。关注sslmode=require。与 Heroku Postgres 的连接要求您使用 SSL,这是某些 PostgreSQL 客户端(例如 Navicat)中的“高级”选项。例如:

    $ psql postgres://abcdefghijklmn:abcdefghijklmnopqrstuvwxyz@ec2-1-2-3-4.compute-1.amazonaws.com:5432/abcdefghijklmn
    

    更多信息在这里:https://devcenter.heroku.com/articles/heroku-postgresql#external-connections-ingress

    【讨论】: