postgresql不接受端口5432上的tcp / ip for linux

时间:2018-02-13 21:08:51

标签: ruby-on-rails linux database postgresql

我尝试在linux上安装psql但遇到了一些问题。我从yaourt下载并且psql命令运行没有任何问题。在我尝试运行rails db:setup之前,这一切似乎都运行正常,此时我抛出了错误:

could not connect to server: Connection timed out
    Is the server running on host "localhost" (92.242.132.16) and accepting
    TCP/IP connections on port 5432?
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "username"=>"revisionhubadmin", "password"=>"password", "host"=>"localhost", "database"=>"revisionHub_development"}
rails aborted!
PG::ConnectionBad: could not connect to server: Connection timed out
    Is the server running on host "localhost" (92.242.132.16) and accepting
    TCP/IP connections on port 5432?
Traceback (most recent call last):
bin/rails: undefined method `reject' for nil:NilClass (NoMethodError)

尽管psql命令运行正常,以及来自systemctl status postgresql.service的以下回复

● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-02-13 16:05:40 GMT; 2h 31min ago
  Process: 1961 ExecStop=/usr/bin/pg_ctl -s -D ${PGROOT}/data stop -m fast (code=exited, status=1/FAILURE)
  Process: 2084 ExecStart=/usr/bin/pg_ctl -s -D ${PGROOT}/data start -w -t 120 (code=exited, status=0/SUCCESS)
  Process: 2082 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGROOT}/data (code=exited, status=0/SUCCESS)
 Main PID: 2086 (postgres)
    Tasks: 5 (limit: 4915)
   CGroup: /system.slice/postgresql.service
           ├─2086 /usr/bin/postgres -D /var/lib/postgres/data
           ├─2088 postgres: checkpointer process   
           ├─2089 postgres: writer process   
           ├─2090 postgres: wal writer process   
           └─2091 postgres: bgworker: logical replication launcher   

Feb 13 16:05:40 bens-xps postgres[2084]: 2018-02-13 16:05:40.524 GMT [2086] LOG:  listening on IPv4 address "0.0.0.0", port 5432
Feb 13 16:05:40 bens-xps postgres[2084]: 2018-02-13 16:05:40.524 GMT [2086] LOG:  listening on IPv6 address "::", port 5432
Feb 13 16:05:40 bens-xps postgres[2084]: 2018-02-13 16:05:40.526 GMT [2086] LOG:  listening on Unix socket "/run/postgresql/.s.PGSQL.5432"
Feb 13 16:05:40 bens-xps postgres[2084]: 2018-02-13 16:05:40.554 GMT [2086] LOG:  could not bind socket for statistics collector: Cannot assign requested address
Feb 13 16:05:40 bens-xps postgres[2084]: 2018-02-13 16:05:40.554 GMT [2086] LOG:  disabling statistics collector for lack of working socket
Feb 13 16:05:40 bens-xps postgres[2084]: 2018-02-13 16:05:40.554 GMT [2086] WARNING:  autovacuum not started because of misconfiguration
Feb 13 16:05:40 bens-xps postgres[2084]: 2018-02-13 16:05:40.554 GMT [2086] HINT:  Enable the "track_counts" option.
Feb 13 16:05:40 bens-xps postgres[2084]: 2018-02-13 16:05:40.556 GMT [2087] LOG:  database system was shut down at 2018-02-13 16:05:17 GMT
Feb 13 16:05:40 bens-xps postgres[2084]: 2018-02-13 16:05:40.561 GMT [2086] LOG:  database system is ready to accept connections
Feb 13 16:05:40 bens-xps systemd[1]: Started PostgreSQL database server.

我已经尝试将我的pg_hba.config更改为

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             0.0.0.0/0            trust
# IPv6 local connections:
host    all             all             ::0/0                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

并使用listen_addresses = '*'将本地地址更改为*,并在postgresql.conf中取消注释port = 5432

这些文件来自/var/lib/postgres/data,我认为这很奇怪,因为which psql返回/usr/bin/psql但系统确认我编辑了正确的文件,因为日志提到了0.0.0.0/0

我在antergos上下载了yaourt的postgres,这是一个基于arch的linux发行版。 PG在版本0.18.4上添加到我的gemfile中,postgres作为版本10.1安装。

我运行应用程序的heroku服务器我正在尝试运行db:生产中的安装工作正常没有任何问题,并且该应用程序也在以前安装的Linux(Ubuntu)上同样的笔记本电脑上运行良好一个mac,暗示问题可能不是这个特定的rails应用程序。无论如何,这是我的database.yml文件的默认和dev部分:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: revisionhubadmin
  password: **********
  host: localhost

development:
  <<: *default
  database: revisionHub_development

是的,我已经创建了一个revisionhubadmin psql帐户。在psql中运行\ du给出:

ben              | Superuser, Create DB

postgres         | Superuser, Create role, Create DB, Replication, Bypass RLS

revisionhubadmin | Superuser, Create DB

所以是的,我有点困惑。有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

几个步骤 - 首先,在数据库服务器上,确保套接字已打开并正在侦听。 ssnetstat之类的内容会对此有所帮助:

$ ss -ltn
State   Recv-Q  Send-Q  Local Address:Port  Peer Address:Port              
LISTEN       0     128  0.0.0.0:5432        0.0.0.0:*

如果它被确认为聆听,我怀疑它是psql适用于您,并且此日志清楚地显示:

LOG:  listening on IPv4 address "0.0.0.0", port 5432

它是从rails系统到数据库本身,还是托管在同一系统上?注意,如果它在同一系统上,psql将使用UNIX域套接字而不是实际的网络环回设备,因此两者之间的通信方式存在显着差异,其中第一个示例将严格使用文件套接字和第二个将使用环回设备:

$ psql -U myuser -d mydb

$ psql -U myuser -d mydb -h 127.0.0.1

如果第二个失败,您确实存在网络连接问题,并且您必须查看防火墙规则,连接等。

如果他们在不同的系统上运行,psql从rails客户端主机运行到数据库主机,但我们仍然无法进行迁移,我唯一的另一个想法就是你可以测试使用telnet或其他一些简单的套接字工具连接到套接字以查看它是否真的有效:

$ telnet mydbhost 5432
编辑:我从配置中注意到数据库是本地的 - 我敢打赌psql在这种情况下使用UNIX套接字,而不是像rails命令那样的网络套接字。验证套接字是否正在侦听,并且可以与网络客户端连接。

编辑2:如果您不想对其进行故障排除,可以直接从database.yml中删除“host:localhost”,这样就可以使用postgres的UNIX套接字了。