Postgresql:无法启动服务数据库系统已关闭

Postgresql:无法启动服务数据库系统已关闭

我使用安装 Postgresql

sudo apt install postgresql postgresql-contrib

然后我跑

sudo systemctl restart postgresql

然后我跑

sudo systemctl status postgresql

它说

Active: active (exited) since ... 1s ago

因此看起来重启命令立即退出。

/var/log/postgresql/postgresql-12-main.log

2020-08-13 23:58:39.553 MSK [47081] LOG:  received fast shutdown request
2020-08-13 23:58:39.607 MSK [47081] LOG:  aborting any active transactions
2020-08-13 23:58:39.613 MSK [47081] LOG:  background worker "logical replication launcher" (PID 47088) exited with exit code 1
2020-08-13 23:58:39.613 MSK [47083] LOG:  shutting down
2020-08-13 23:58:39.888 MSK [47081] LOG:  database system is shut down
2020-08-13 23:58:40.246 MSK [47200] LOG:  starting PostgreSQL 12.2 (Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-8ubuntu1) 9.3.0, 64-bit
2020-08-13 23:58:40.247 MSK [47200] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-08-13 23:58:40.293 MSK [47200] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-08-13 23:58:40.470 MSK [47201] LOG:  database system was shut down at 2020-08-13 23:58:39 MSK
2020-08-13 23:58:40.508 MSK [47200] LOG:  database system is ready to accept connections

/etc/init.d/postgresql

#!/bin/sh
set -e

### BEGIN INIT INFO
# Provides:     postgresql
# Required-Start:   $local_fs $remote_fs $network $time
# Required-Stop:    $local_fs $remote_fs $network $time
# Should-Start:     $syslog
# Should-Stop:      $syslog
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    PostgreSQL RDBMS server
### END INIT INFO

# Setting environment variables for the postmaster here does not work; please
# set them in /etc/postgresql/<version>/<cluster>/environment instead.

[ -r /usr/share/postgresql-common/init.d-functions ] || exit 0

. /usr/share/postgresql-common/init.d-functions

# versions can be specified explicitly
if [ -n "$2" ]; then
    versions="$2 $3 $4 $5 $6 $7 $8 $9"
else
    get_versions
fi

case "$1" in
    start|stop|restart|reload)
        if [ "$1" = "start" ]; then
            create_socket_directory
        fi
    if [ -z "`pg_lsclusters -h`" ]; then
        log_warning_msg 'No PostgreSQL clusters exist; see "man pg_createcluster"'
        exit 0
    fi
    for v in $versions; do
        $1 $v || EXIT=$?
    done
    exit ${EXIT:-0}
        ;;
    status)
    LS=`pg_lsclusters -h`
    # no clusters -> unknown status
    [ -n "$LS" ] || exit 4
    echo "$LS" | awk 'BEGIN {rc=0} {if (match($4, "down")) rc=3; printf ("%s/%s (port %s): %s\n", $1, $2, $3, $4)}; END {exit rc}'
    ;;
    force-reload)
    for v in $versions; do
        reload $v
    done
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload|force-reload|status} [version ..]"
        exit 1
        ;;
esac

exit 0

我如何解决它?

附言:我遇到的问题是 Postgresql不会开始.restart执行命令后其状态应为 ,active (running)而 则为active (exited)

我尝试清除 postgresql 并运行

sudo apt-get install postgresql

再次,这是 /var/log/postgresql/postgresql-12-main.log:

2020-08-13 23:53:33.849 MSK [44882] LOG:  starting PostgreSQL 12.2 (Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-8ubuntu1) 9.3.0, 64-bit
2020-08-13 23:53:33.850 MSK [44882] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-08-13 23:53:33.898 MSK [44882] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-08-13 23:53:34.042 MSK [44883] LOG:  database system was shut down at 2020-08-13 23:53:30 MSK
2020-08-13 23:53:34.092 MSK [44882] LOG:  database system is ready to accept connections
2020-08-13 23:56:17.242 MSK [44882] LOG:  received fast shutdown request
2020-08-13 23:56:17.462 MSK [44882] LOG:  aborting any active transactions
2020-08-13 23:56:17.466 MSK [44882] LOG:  background worker "logical replication launcher" (PID 44889) exited with exit code 1
2020-08-13 23:56:17.468 MSK [44884] LOG:  shutting down
2020-08-13 23:56:17.750 MSK [44882] LOG:  database system is shut down
2020-08-13 23:56:18.153 MSK [47081] LOG:  starting PostgreSQL 12.2 (Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-8ubuntu1) 9.3.0, 64-bit
2020-08-13 23:56:18.154 MSK [47081] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-08-13 23:56:18.206 MSK [47081] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-08-13 23:56:18.392 MSK [47082] LOG:  database system was shut down at 2020-08-13 23:56:17 MSK
2020-08-13 23:56:18.455 MSK [47081] LOG:  database system is ready to accept connections
2020-08-13 23:58:39.553 MSK [47081] LOG:  received fast shutdown request
2020-08-13 23:58:39.607 MSK [47081] LOG:  aborting any active transactions
2020-08-13 23:58:39.613 MSK [47081] LOG:  background worker "logical replication launcher" (PID 47088) exited with exit code 1
2020-08-13 23:58:39.613 MSK [47083] LOG:  shutting down
2020-08-13 23:58:39.888 MSK [47081] LOG:  database system is shut down
2020-08-13 23:58:40.246 MSK [47200] LOG:  starting PostgreSQL 12.2 (Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-8ubuntu1) 9.3.0, 64-bit
2020-08-13 23:58:40.247 MSK [47200] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-08-13 23:58:40.293 MSK [47200] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-08-13 23:58:40.470 MSK [47201] LOG:  database system was shut down at 2020-08-13 23:58:39 MSK
2020-08-13 23:58:40.508 MSK [47200] LOG:  database system is ready to accept connections
2020-08-14 00:05:28.033 MSK [47200] LOG:  received fast shutdown request
2020-08-14 00:05:28.084 MSK [47200] LOG:  aborting any active transactions
2020-08-14 00:05:28.090 MSK [47200] LOG:  background worker "logical replication launcher" (PID 47207) exited with exit code 1
2020-08-14 00:05:28.092 MSK [47202] LOG:  shutting down
2020-08-14 00:05:28.290 MSK [47200] LOG:  database system is shut down
2020-08-14 00:05:28.781 MSK [47452] LOG:  starting PostgreSQL 12.2 (Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-8ubuntu1) 9.3.0, 64-bit
2020-08-14 00:05:28.782 MSK [47452] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-08-14 00:05:28.837 MSK [47452] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-08-14 00:05:28.990 MSK [47453] LOG:  database system was shut down at 2020-08-14 00:05:28 MSK
2020-08-14 00:05:29.031 MSK [47452] LOG:  database system is ready to accept connections
2020-08-14 00:15:05.696 MSK [47452] LOG:  received fast shutdown request
2020-08-14 00:15:05.747 MSK [47452] LOG:  aborting any active transactions
2020-08-14 00:15:05.754 MSK [47452] LOG:  background worker "logical replication launcher" (PID 47459) exited with exit code 1
2020-08-14 00:15:05.755 MSK [47454] LOG:  shutting down
2020-08-14 00:15:05.953 MSK [47452] LOG:  database system is shut down
2020-08-14 00:15:06.204 MSK [48037] LOG:  starting PostgreSQL 12.2 (Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-8ubuntu1) 9.3.0, 64-bit
2020-08-14 00:15:06.205 MSK [48037] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-08-14 00:15:06.257 MSK [48037] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-08-14 00:15:06.452 MSK [48039] LOG:  database system was shut down at 2020-08-14 00:15:05 MSK
2020-08-14 00:15:06.503 MSK [48037] LOG:  database system is ready to accept connections
2020-08-14 19:00:11.997 MSK [48037] LOG:  could not open file "postmaster.pid": No such file or directory
2020-08-14 19:00:12.032 MSK [48037] LOG:  performing immediate shutdown because data directory lock file is invalid
2020-08-14 19:00:12.032 MSK [48037] LOG:  received immediate shutdown request
2020-08-14 19:00:12.032 MSK [48037] LOG:  could not open file "postmaster.pid": No such file or directory
2020-08-14 19:00:12.063 MSK [48044] LOG:  could not open temporary statistics file "pg_stat/global.tmp": No such file or directory
2020-08-14 19:00:12.063 MSK [48043] WARNING:  terminating connection because of crash of another server process
2020-08-14 19:00:12.063 MSK [48043] DETAIL:  The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2020-08-14 19:00:12.063 MSK [48043] HINT:  In a moment you should be able to reconnect to the database and repeat your command.
2020-08-14 19:00:12.130 MSK [79907] WARNING:  terminating connection because of crash of another server process
2020-08-14 19:00:12.130 MSK [79907] DETAIL:  The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2020-08-14 19:00:12.130 MSK [79907] HINT:  In a moment you should be able to reconnect to the database and repeat your command.
2020-08-14 19:00:12.169 MSK [48037] LOG:  database system is shut down
2020-08-14 19:09:25.177 MSK [84877] LOG:  starting PostgreSQL 12.2 (Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-8ubuntu1) 9.3.0, 64-bit
2020-08-14 19:09:25.179 MSK [84877] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-08-14 19:09:25.226 MSK [84877] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-08-14 19:09:25.370 MSK [84878] LOG:  database system was shut down at 2020-08-14 19:09:21 MSK
2020-08-14 19:09:25.410 MSK [84877] LOG:  database system is ready to accept connections

答案1

当您说您清除了 PostgreSQL 时,您的意思是您做了类似的事情吗sudo apt-get --purge remove postgresql-12?如果没有该--purge标志,数据库文件将不会被删除,只有管理这些文件的软件将被删除。

日志表明有什么东西触发了快速关机请求,根据 PostgreSQL 文档,只有当另一个进程发送 SIGINT 信号时才会发生这种情况(数值 2)。在数据库准备好接受连接后大约 2 或 3 分钟,某些进程正在执行此操作。因此,您的服务器似乎确实启动了,但退出得非常快……您需要找出执行此操作的进程。这可能是一个恶意进程、Cron 作业或启动 PostgreSQL 的 systemctl 进程。

如果是 systemctl 进程(正在启动 PostgreSQL)将其终止,那是因为它无法检测到 PostgreSQL 是否已成功启动。这可能是由于 PostgreSQL 中的配置问题或数据损坏。尝试增加日志记录的详细程度或尝试手动启动 PostgreSQL 服务器(sudo -u postgres /usr/lib/postgresql/12/bin/postgres -D /var/lib/postgresql/12/main -c config_file=/etc/postgresql/12/main/postgresql.conf)以查看是否能找到线索。

或者,如果您愿意删除所有数据,您可以卸载 PostgreSQL,然后删除/var/lib/postgresql/12和中仍然存在的任何内容/etc/postgresql/12。然后执行sudo apt-get install postgresql-12,您应该会再次获得全新安装。

如果它是一个 roges 进程或 Cron 作业或类似的东西,您需要找到向 PostgreSQL 服务器发出信号的进程的 PID,然后修复它。要找出哪个进程正在向 PostgreSQL 服务器发出信号,您可以使用 killsnoop-bpfcc ( sudo apt-get install bpfcc-tools)。在终端中启动它,然后等待您的 PostgreSQL 再次死机。您应该看到一行,其中 SIG 列为 2。TPID 列是收到信号的进程,PID 列是正在调皮的进程。

例如:

  1. sleep 100000从控制台开始。
  2. 在另一个控制台中,我检查了它的 PID:。ps -Alf | grep sleep我发现它是 31326。
  3. 我启动killsnoop-bpfcc并检查 TPID 31326。
  4. 在另一个控制台中,我终止了睡眠命令:kill -SIGINT 31326

结果是:

TIME      PID    COMM             SIG  TPID   RESULT
20:29:43  9097   bash             2    31326  0

因此,进程 9097 向进程 31326 发出了 SIGINT (2) 信号。

相关内容