【问题标题】:How to connect to postgres in Linode from local system如何从本地系统连接到 Linode 中的 postgres
【发布时间】:2022-01-22 19:48:44
【问题描述】:

我有一个 python 脚本可以将数据添加到 postgres 数据库。在测试期间,我在本地系统中创建了一个数据库,它运行良好。现在我的数据库在 linode 中运行。我不确定如何从本地系统连接到数据库。我将数据库详细信息存储在 python 文件中,如下所示

DATABASE = 'my_database'
HOST = <IP_ADDRESS> # the ip_address of the linode instance
PORT = '5432'
USER = 'database_user'
PASSWORD = 'database_password'

我使用上面的代码访问数据库。如果数据库在本地主机中运行,则主机等于HOST = 'localhost'

当我从本地系统运行 python 脚本连接到数据库时,这是我得到的消息

psycopg2.OperationalError: connection to server at "<IP_ADDESS>", port 5432 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?

我不知道如何连接到在 linode 中运行的数据库。

【问题讨论】:

  • A) postgresql.conf 中的服务器 listen_addresses 设置不正确。请参阅Connections 和/或 B) 有防火墙阻止端口 5432

标签: python postgresql linode


【解决方案1】:

我通过编辑pg_hba.conf 解决了这个问题。

  1. 在 linode 实例中,导航到 /var/lib/pgsql/data

  2. 在目录中,打开pg_hba.conf并在IPv4本地连接下添加以下行

host    all             all             <local system's Public IP ADDRESS>/32   md5
  1. 保存文件并重新启动 postgres。
  2. 启动到 linode 实例的 SSH 隧道
  3. 将数据库详细信息中的主机和端口更改为127.0.0.1,以及在隧道期间选择的端口。就我而言,它是5433
  4. 现在运行 python 脚本应该使本地系统能够连接到在 linode 实例中运行的 postgres 服务器。

【讨论】:

  • 不,你是通过防火墙隧道解决的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
相关资源
最近更新 更多