![](/img/trans.png)
[英]Rails in linux. Is the server running on host “localhost” (::1) and accepting TCP/IP connections on port 5432?
[英]postgresql not accepting tcp/ip on port 5432 for rails on linux
我尝试在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
并将postgresql.conf中的listen_addresses = '*'
和取消注释port = 5432
更改为*。
这些文件来自/var/lib/postgres/data
,我认为这很奇怪,因为which psql
返回/usr/bin/psql
但是systemctl确认我编辑了正确的文件,因为日志提到了0.0.0.0/0
我在antgos上用yaourt下载了postgres,这是一个基于Arch的Linux发行版。 PG已添加到版本0.18.4的我的gemfile中,而postgres已安装为版本10.1。
我的Heroku服务器在生产环境中运行我要在其上运行db:setup的应用程序,但运行正常,没有任何问题,并且该应用程序也可以在同一台笔记本电脑和Mac上的先前安装的linux(Ubuntu)上运行良好,提示此特定Rails应用可能不存在此问题。 无论如何,这是我的database.yml文件的默认和dev部分:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: revisionhubadmin
password: **********
host: localhost
development:
<<: *default
database: revisionHub_development
是的,我已经创建了一个versionhubadmin psql帐户。 在psql中运行\\ du可以得到:
ben | Superuser, Create DB
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS
revisionhubadmin | Superuser, Create DB
是的,我有点困惑。 有谁知道如何解决这一问题?
几个步骤-首先,在数据库服务器上,确保套接字已打开并正在侦听。 诸如ss
或netstat
类的东西可以帮助您:
$ 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套接字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.