【问题标题】:Amazon Ubuntu Postgres - Won't Connect to LocalhostAmazon Ubuntu Postgres - 不会连接到本地主机
【发布时间】:2018-02-05 02:42:08
【问题描述】:

我正在尝试使用以下引擎字符串连接到 postgres: sql_alchemy_conn = postgresql://username:password@localhost:5439/dbname

我收到以下错误:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "172.31.43.180" and accepting
TCP/IP connections on port 5439?

我知道服务正在运行:

sudo service postgresql status

返回:

Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Sun 2017-08-27 19:21:06 UTC; 4min 21s ago
Process: 1310 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 1310 (code=exited, status=0/SUCCESS)
Tasks: 0
Memory: 0B
CPU: 0
CGroup: /system.slice/postgresql.service

Aug 27 19:21:06 ip-loc-al-ip-add systemd[1]: Starting PostgreSQL RDBMS...
Aug 27 19:21:06 ip-loc-al-ip-add systemd[1]: Started PostgreSQL RDBMS.

此外,它似乎在 localhost 上进行侦听('我也尝试过为其提供 ip 而不仅仅是 dns),至少:

netstat -anpt | grep LISTEN

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -               
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      -               
tcp6       0      0 :::22                   :::*                    LISTEN      -   
tcp6       0      0 :::5432                 :::*                    LISTEN      -   

另外,配置文件中有以下内容:

postgresql.conf: # - 连接设置 -

listen_addresses = '*'         # what IP address(es) to listen on;
                                # comma-separated list of addresses;
                                # defaults to 'localhost'; use '*' for all
                                # (change requires restart)
port = 5432                     # (change requires restart)

pg_hba.conf:

local   all             all                                trust

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             127.0.0.1/24            md5

# IPv6 local connections:
host    all             all             ::1/128                 md5

最后,我不需要或不希望从外部连接访问它;这只是一个本地访问的数据库。

【问题讨论】:

  • 服务器正在监听 5432 端口,您正在尝试连接到 5439。
  • 除了@PauloAlmeida 的出色发现之外,您可能还需要考虑使用 Unix 域套接字;更少的安全隐患(当您在 localhost、v4 和 v6 上收听时看到)并且您在没有 IP 开销的情况下获得大约两倍的性能。 cybertec.at/…
  • 当然是那么愚蠢。谢谢。

标签: python linux postgresql ubuntu amazon-ec2


【解决方案1】:

您的端口号有误 你应该连接 postgresql://username:password@localhost:5432/dbname 而不是 postgresql://username:password@localhost:5439/dbname 54

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-28
    • 2019-03-25
    • 1970-01-01
    • 2011-09-08
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多