【问题标题】:Postgresql can't connect to server?Postgresql无法连接到服务器?
【发布时间】:2020-01-24 21:47:17
【问题描述】:

我是 postgresql 和 rails 的新手。我正在尝试连接到服务器并创建必要的数据库。当我运行 rake db:create 时,我会收到以下消息:

could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Couldn't create 'ecom_shoppie_development' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

但是服务器在端口 5433 上运行。我尝试更改 postgresql.conf 和 database.yml 中的端口号,但我得到了同样的错误。

我已经尝试卸载并重新安装 postgresql,但它总是选择端口 5433 而不是 5432。

我关注了这个tutorial

Postgresql 日志文件 /var/log/postgresql/postgresql-11-main.log:

2019-09-24 06:12:09.951 EDT [3379] LOG:  listening on IPv4 address "127.0.0.1", port 5433
2019-09-24 06:12:10.087 EDT [3379] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5433"
2019-09-24 06:12:10.669 EDT [3380] LOG:  database system was shut down at 2019-09-24 05:54:16 EDT
2019-09-24 06:12:11.313 EDT [3379] LOG:  database system is ready to accept connections
2019-09-24 06:12:12.102 EDT [3387] [unknown]@[unknown] LOG:  incomplete startup packet
2019-09-24 06:35:51.836 EDT [3379] LOG:  received fast shutdown request
2019-09-24 06:35:51.887 EDT [3379] LOG:  aborting any active transactions
2019-09-24 06:35:52.001 EDT [3379] LOG:  background worker "logical replication launcher" (PID 3386) exited with exit code 1
2019-09-24 06:35:52.003 EDT [3381] LOG:  shutting down
2019-09-24 06:35:52.842 EDT [3379] LOG:  database system is shut down
2019-09-24 06:35:54.193 EDT [3475] LOG:  could not bind IPv4 address "127.0.0.1": Permission denied
2019-09-24 06:35:54.193 EDT [3475] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2019-09-24 06:35:54.194 EDT [3475] WARNING:  could not create listen socket for "localhost"
2019-09-24 06:35:54.194 EDT [3475] FATAL:  could not create any TCP/IP sockets
2019-09-24 06:35:54.215 EDT [3475] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.
2019-09-24 06:36:57.130 EDT [3517] LOG:  could not bind IPv4 address "127.0.0.1": Permission denied
2019-09-24 06:36:57.130 EDT [3517] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2019-09-24 06:36:57.130 EDT [3517] WARNING:  could not create listen socket for "localhost"
2019-09-24 06:36:57.131 EDT [3517] FATAL:  could not create any TCP/IP sockets
2019-09-24 06:36:57.153 EDT [3517] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.
2019-09-24 06:38:02.555 EDT [3533] LOG:  could not bind IPv4 address "127.0.0.1": Permission denied
2019-09-24 06:38:02.555 EDT [3533] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2019-09-24 06:38:02.556 EDT [3533] WARNING:  could not create listen socket for "localhost"
2019-09-24 06:38:02.557 EDT [3533] FATAL:  could not create any TCP/IP sockets
2019-09-24 06:38:02.579 EDT [3533] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.
2019-09-24 06:42:48.723 EDT [3564] LOG:  could not bind IPv4 address "127.0.0.1": Permission denied
2019-09-24 06:42:48.723 EDT [3564] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2019-09-24 06:42:48.724 EDT [3564] WARNING:  could not create listen socket for "localhost"
2019-09-24 06:42:48.724 EDT [3564] FATAL:  could not create any TCP/IP sockets
2019-09-24 06:42:48.746 EDT [3564] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.
2019-09-24 06:44:22.342 EDT [3599] LOG:  listening on IPv4 address "127.0.0.1", port 5433
2019-09-24 06:44:22.463 EDT [3599] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5433"
2019-09-24 06:44:23.036 EDT [3600] LOG:  database system was shut down at 2019-09-24 06:35:52 EDT
2019-09-24 06:44:23.772 EDT [3599] LOG:  database system is ready to accept connections
2019-09-24 06:44:24.488 EDT [3607] [unknown]@[unknown] LOG:  incomplete startup packet
2019-09-24 06:49:23.812 EDT [3601] WARNING:  could not flush dirty data: Function not implemented
2019-09-24 08:43:17.387 EDT [3599] LOG:  received fast shutdown request
2019-09-24 08:43:17.446 EDT [3599] LOG:  aborting any active transactions
2019-09-24 08:43:17.693 EDT [3599] LOG:  background worker "logical replication launcher" (PID 3606) exited with exit code 1
2019-09-24 08:43:17.695 EDT [3601] LOG:  shutting down
2019-09-24 08:43:18.620 EDT [3599] LOG:  database system is shut down
2019-09-24 08:43:20.185 EDT [6822] LOG:  could not bind IPv4 address "127.0.0.1": Permission denied
2019-09-24 08:43:20.185 EDT [6822] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2019-09-24 08:43:20.186 EDT [6822] WARNING:  could not create listen socket for "localhost"
2019-09-24 08:43:20.186 EDT [6822] FATAL:  could not create any TCP/IP sockets
2019-09-24 08:43:20.206 EDT [6822] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.
2019-09-24 08:44:24.969 EDT [6857] LOG:  could not bind IPv4 address "0.0.0.0": Address already in use
2019-09-24 08:44:24.969 EDT [6857] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2019-09-24 08:44:24.969 EDT [6857] LOG:  could not bind IPv6 address "::": Address already in use
2019-09-24 08:44:24.969 EDT [6857] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2019-09-24 08:44:24.969 EDT [6857] WARNING:  could not create listen socket for "*"
2019-09-24 08:44:24.970 EDT [6857] FATAL:  could not create any TCP/IP sockets
2019-09-24 08:44:24.991 EDT [6857] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.
2019-09-24 08:48:02.243 EDT [6894] LOG:  listening on IPv4 address "127.0.0.1", port 5433
2019-09-24 08:48:02.396 EDT [6894] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5433"
2019-09-24 08:48:03.677 EDT [6895] LOG:  database system was shut down at 2019-09-24 08:43:17 EDT
2019-09-24 08:48:04.398 EDT [6894] LOG:  database system is ready to accept connections
2019-09-24 08:48:05.177 EDT [6902] [unknown]@[unknown] LOG:  incomplete startup packet
2019-09-24 08:53:04.168 EDT [6896] WARNING:  could not flush dirty data: Function not implemented
2019-09-24 15:10:39.731 EDT [7350] tjgrevious@tjgrevious FATAL:  password authentication failed for user "tjgrevious"
2019-09-24 15:10:39.731 EDT [7350] tjgrevious@tjgrevious DETAIL:  Password does not match for user "tjgrevious".
        Connection matched pg_hba.conf line 92: "host    all             all             127.0.0.1/32            md5"
2019-09-24 15:10:39.926 EDT [7351] tjgrevious@tjgrevious FATAL:  password authentication failed for user "tjgrevious"
2019-09-24 15:10:39.926 EDT [7351] tjgrevious@tjgrevious DETAIL:  Password does not match for user "tjgrevious".
        Connection matched pg_hba.conf line 92: "host    all             all             127.0.0.1/32            md5"
2019-09-24 15:10:53.402 EDT [7355] tjgrevious@tjgrevious FATAL:  database "tjgrevious" does not exist
2019-09-24 15:11:06.360 EDT [7358] tjgrevious@tjgrevious FATAL:  password authentication failed for user "tjgrevious"
2019-09-24 15:11:06.360 EDT [7358] tjgrevious@tjgrevious DETAIL:  Password does not match for user "tjgrevious".
        Connection matched pg_hba.conf line 92: "host    all             all             127.0.0.1/32            md5"
2019-09-24 15:11:06.478 EDT [7359] tjgrevious@tjgrevious FATAL:  password authentication failed for user "tjgrevious"
2019-09-24 15:11:06.478 EDT [7359] tjgrevious@tjgrevious DETAIL:  Password does not match for user "tjgrevious".
        Connection matched pg_hba.conf line 92: "host    all             all             127.0.0.1/32            md5"
2019-09-24 15:11:40.716 EDT [7374] tjgrevious@tjgrevious FATAL:  password authentication failed for user "tjgrevious"
2019-09-24 15:11:40.716 EDT [7374] tjgrevious@tjgrevious DETAIL:  Password does not match for user "tjgrevious".
        Connection matched pg_hba.conf line 92: "host    all             all             127.0.0.1/32            md5"
2019-09-24 15:11:40.853 EDT [7375] tjgrevious@tjgrevious FATAL:  password authentication failed for user "tjgrevious"
2019-09-24 15:11:40.853 EDT [7375] tjgrevious@tjgrevious DETAIL:  Password does not match for user "tjgrevious".
        Connection matched pg_hba.conf line 92: "host    all             all             127.0.0.1/32            md5"
2019-09-24 15:12:02.098 EDT [7378] tjgrevious@tjgrevious FATAL:  database "tjgrevious" does not exist

【问题讨论】:

  • “我尝试在 postgresql.conf 和 database.yml 中更改端口号,但我得到了同样的错误。”如果错误没有变为报告 5433 而不是 5432,那么您对 ​​database.yml 的更改一定没有生效。

标签: ruby-on-rails postgresql psql


【解决方案1】:

您确定它在 5433 上运行吗?您可以使用 netstat 工具查看实际运行的内容(在 OS X、Windows 和 Linux 上可用,命令行语法在这三个工具中有所不同)

如果您在 Mac 上,并且正在使用默认的 postgres 安装,请放弃它并通过自制软件安装它。

最终发生的情况是用户安装 Pg 并从一个包启动服务器,但使用不同包的 psql 和 libpq 客户端。通常,当他们运行 Postgres.app 或 homebrew Pg 并与操作系统附带的 psql 连接时,就会发生这种情况。不仅这些有时具有不同的默认端口,而且 Mac OS X 附带的 Pg 具有不同的默认 unix 套接字路径,因此即使服务器在同一端口上运行,它也不会侦听相同的 unix 套接字。

大多数 Mac 用户只需使用 tcp/ip 和 psql -h localhost 即可解决此问题。如果需要,您还可以指定一个端口,例如 psql -h localhost -p 5433。您可能有多个 PostgreSQL 实例正在运行,因此请确保您使用 select version() 和 SHOW data_directory; 连接到正确的实例。

更多答案请见postgresql port confusion 5433 or 5432?

【讨论】:

  • 我正在使用 ubuntu。 netstat 不返回任何内容。我试过sudo netstat -plunt | grep postgres。 psql 命令也给出了无法连接到服务器的错误。 service postgresql status 返回:11/main (port 5433): online。这是我检查端口的唯一方法。
  • @Zuse 听起来 PostgreSQL 并没有真正启动,尽管“服务”说了什么。您能找到 PostgresSQL 日志文件并查看它的内容吗?
  • 日志文件太长所以我把它添加到问题中
【解决方案2】:

试试这个:

$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql

参考:https://stackoverflow.com/a/18832331/13180074

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-08
    • 2017-08-02
    • 2017-03-03
    • 2012-10-10
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2021-02-27
    相关资源
    最近更新 更多