Amazon Ubuntu Postgres - 无法连接到Localhost

时间:2017-08-27 19:39:42

标签: python linux postgresql ubuntu amazon-ec2

我正在尝试使用以下引擎字符串连接到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

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

1 个答案:

答案 0 :(得分:0)

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