【问题标题】:PostgreSQL : 'psql: error: could not connect to server: No such file or directory' .s.PGSQL.5432PostgreSQL:'psql:错误:无法连接到服务器:没有这样的文件或目录'.s.PGSQL.5432
【发布时间】:2021-04-08 18:44:44
【问题描述】:

从昨天开始,我在 Ubuntu 20.04 - PostgreSQL 12 上运行 psql 时出现错误。 这是错误:

psql: error: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

我已经在互联网上看到了很多关于这个问题的答案,但没有人工作过......

我在安装 phppgadmin 后重新启动 postgresql 时发生了这种情况,这是最新的日志:

2021-01-01 21:37:27.981 UTC [1071608] LOG:  received fast shutdown request
2021-01-01 21:37:27.982 UTC [1071608] LOG:  aborting any active transactions
2021-01-01 21:37:27.982 UTC [434049] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:27.982 UTC [1514704] thegabdoosan@ephedia_web FATAL:  terminating connection due to administrator command
2021-01-01 21:37:27.984 UTC [1231171] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:27.986 UTC [1231170] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:27.988 UTC [899543] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:27.990 UTC [899542] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:27.992 UTC [899541] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:27.994 UTC [899540] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:27.996 UTC [899539] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:27.998 UTC [899538] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:27.999 UTC [899537] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:28.001 UTC [899536] thegabdoosan@ephedia FATAL:  terminating connection due to administrator command
2021-01-01 21:37:28.009 UTC [1071608] LOG:  background worker "logical replication launcher" (PID 1071615) exited with exit code 1
2021-01-01 21:37:28.010 UTC [1071610] LOG:  shutting down
2021-01-01 21:37:28.030 UTC [1071608] LOG:  database system is shut down

我没有看到任何奇怪的东西

  • pg_hba.conf:
# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

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

  • postgresql.conf
# - Connection Settings -

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)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories

当我尝试运行 psql -h localhost 时出现另一个错误:

psql: error: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

当我运行sudo systemctl status postgresql 时:

● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Sat 2021-01-02 09:10:59 UTC; 18min ago
    Process: 1750585 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 1750585 (code=exited, status=0/SUCCESS)

janv. 02 09:10:59 vps-d989390a systemd[1]: Starting PostgreSQL RDBMS...
janv. 02 09:10:59 vps-d989390a systemd[1]: Finished PostgreSQL RDBMS.

当我运行 ls /var/run/postgresql/ -a 时:

0 drwxrwsr-x  3 postgres postgres   80 janv.  1 22:53 .
0 drwxr-xr-x 32 root     root     1060 janv.  2 09:09 ..
0 drwxr-s---  2 postgres postgres   40 janv.  1 21:37 12-main.pg_stat_tmp
0 lrwxrwxrwx  1 root     postgres   18 janv.  1 22:53 .s.PGSQL.5432 -> /tmp/.s.PGSQL.5432

当我运行sudo pg_ctlcluster 12 main start 时:

Job for postgresql@12-main.service failed because the service did not take the steps required by its unit configuration.
See "systemctl status postgresql@12-main.service" and "journalctl -xe" for details.

pg_lsclusters

Ver Cluster Port Status Owner     Data directory              Log file
12  main    5432 down   <unknown> /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log

当我运行sudo systemctl status postgresql@12-main.service 时:

● postgresql@12-main.service - PostgreSQL Cluster 12-main
     Loaded: loaded (/lib/systemd/system/postgresql@.service; enabled; vendor preset: enabled)
     Active: failed (Result: protocol) since Sat 2021-01-02 13:21:05 UTC; 3h 50min ago
    Process: 705 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 12-main start (code=exited, status=1/FAILURE)

Jan 02 13:21:04 vps-d989390a systemd[1]: Starting PostgreSQL Cluster 12-main...
Jan 02 13:21:05 vps-d989390a postgresql@12-main[723]: Error: Could not open logfile /var/log/postgresql/postgresql-12-main.log
Jan 02 13:21:05 vps-d989390a postgresql@12-main[705]: Error: /usr/lib/postgresql/12/bin/pg_ctl /usr/lib/postgresql/12/bin/pg_ctl start -D /var/lib/postgresql/12/main -l /var/log/postgresql/postgresql-12>
Jan 02 13:21:05 vps-d989390a systemd[1]: postgresql@12-main.service: Can't open PID file /run/postgresql/12-main.pid (yet?) after start: Operation not permitted
Jan 02 13:21:05 vps-d989390a systemd[1]: postgresql@12-main.service: Failed with result 'protocol'.
Jan 02 13:21:05 vps-d989390a systemd[1]: Failed to start PostgreSQL Cluster 12-main.

这是我运行 sudo journalctl -xe 时的最后 35 行 sudo systemctl start postgresql@12-main.servicehttps://mystb.in/TillDimensionIntellectual.yaml

/etc/init.d/postgresql 输出:https://mystb.in/AmountsAlexanderExtreme.bash

我也关闭了ufw

如果我卸载 - 安装 postgresql,我会丢失我的数据库吗?

【问题讨论】:

  • systemctl status postgresql@12-main.service 显示什么?以及journalctl -xe 在您尝试开始之后?将此信息添加到您的问题中。
  • 我加了! ????
  • 看起来是权限错误(Could not open logfile /var/log/postgresql/postgresql-12-main.log)和错误目录(Can't open PID file /run/postgresql/12-main.pid)的某种组合。最后一个应该是/var/run/postgresql/12-main.pid。你是如何安装软件包的?您在机器上安装的类型不止一种吗?
  • 我用 apt-get 安装了这些包。而且我认为我在机器上安装的类型不超过一种:/如果是权限错误,我不能用(a)命令行修复它吗?
  • 我想要/etc/init.d/postgresql的内容(里面有什么)。

标签: database postgresql ubuntu unix psql


【解决方案1】:

锁定文件的位置(或处理)似乎已更改(版本之间?)。我通过编辑启动文件(执行 setuid root)来修复它: sudo vi /etc/init.d/postgresql


# Parse command line parameters.

case $1 in
  start)
        echo -n "Starting PostgreSQL: "
        test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj

        #################################
        # FIX: Directory Lockfile must be writable by postgres
        mkdir -p /var/run/postgresql
        chown postgres.postgres /var/run/postgresql
        ##################################

        #echo su - $PGUSER -c "$DAEMON -D '$PGDATA' &"
        su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1
        echo "ok"
        ;;
  stop)

顺便说一句:unix-domain-socket 有时也存在于该目录中。 (以前是/tmp/

BTW2:我把它放在启动脚本中是因为 /var/run/ 似乎在重启时被擦除了。

BTW3:使用风险自负!

【讨论】:

  • 很确定这不是它。我的设置与@TheGabDooSan 相同,我可以启动 Postgres 12。上面的也不是 Ubuntu 软件包附带的 postgresql 初始化文件。
  • @AdrianKlaver 这是正确的。这是一个旧版本。 (我从源安装)
  • 不幸的是,@TheGabDooSan 设置不是这样,因此不适用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2014-05-10
  • 2018-01-14
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多