为什么我的 Postgres 数据库工作了一段时间,然后在重新启动后无法 "start server"?

Why is my Postgres database working for a while and then not able to "start server" once restarted?

最近,我开始使用旧的 Raspberry Pi 3 b+,我认为在我的本地网络上托管一个 Postgres 数据库并将其用于任何我想工作的地方是一个很好的做法通过。我知道 运行 Postgres 在 Raspberry Pi 上使用 1GB 内存并不理想,可能会对 SD 卡造成影响,但我已经更新了 postgresql.conf 文件并指定了数据目录路径是使用 1TB SSD。此外,我安装了 zram 和 log2ram 以尝试抑制 SD 卡上的一些开销。

我正在使用的技术概述:

昨天,我从 python 笔记本写入 Postgres 数据库没有任何问题,但是一旦我重新启动 Raspberry Pi,我就无法从 DataGrip 访问数据库并且会收到我的终端在 Ubuntu:

中出现以下错误
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"?

我检查了 postgres 服务器的状态,似乎没问题...:[=​​14=]

● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Thu 2021-01-28 13:34:41 UTC; 20min ago
    Process: 1895 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 1895 (code=exited, status=0/SUCCESS)

Jan 28 13:34:41 ubuntu systemd[1]: Starting PostgreSQL RDBMS...
Jan 28 13:34:41 ubuntu systemd[1]: Finished PostgreSQL RDBMS.

这是postgresql-12-main.log中提供的:

2021-01-28 13:17:23.344 UTC [1889] LOG:  starting PostgreSQL 12.5 (Ubuntu 12.5-0ubuntu0.20.04.1) on aarch64-unknown-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit
2021-01-28 13:17:23.362 UTC [1889] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2021-01-28 13:17:23.362 UTC [1889] LOG:  listening on IPv6 address "::", port 5432
2021-01-28 13:17:23.365 UTC [1889] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-01-28 13:17:23.664 UTC [1899] LOG:  database system was shut down at 2021-01-28 01:43:38 UTC
2021-01-28 13:17:24.619 UTC [1899] LOG:  could not link file "pg_wal/xlogtemp.1899" to "pg_wal/000000010000000000000002": Operation not permitted
2021-01-28 13:17:24.670 UTC [1899] FATAL:  could not open file "pg_wal/000000010000000000000002": No such file or directory
2021-01-28 13:17:24.685 UTC [1889] LOG:  startup process (PID 1899) exited with exit code 1
2021-01-28 13:17:24.686 UTC [1889] LOG:  aborting startup due to startup process failure
2021-01-28 13:17:24.708 UTC [1889] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.

如果您有任何问题或希望我提供任何其他信息,请告诉我。感谢您提前提出的任何建议。

配置文件(部分):


#------------------------------------------------------------------------------
# FILE LOCATIONS
#------------------------------------------------------------------------------

# The default values of these variables are driven from the -D command-line
# option or PGDATA environment variable, represented here as ConfigDir.

#data_directory = 'ConfigDir'           # use data in another directory
                                        # (change requires restart)
#hba_file = 'ConfigDir/pg_hba.conf'     # host-based authentication file
                                        # (change requires restart)
#ident_file = 'ConfigDir/pg_ident.conf' # ident configuration file
                                        # (change requires restart)

# If external_pid_file is not explicitly set, no extra PID file is written.
#external_pid_file = ''                 # write an extra PID file
                                        # (change requires restart)

/etc/init.d/postgresql(部分):

注意:这是来自非标准安装。 YMMV


# Data directory
#PGDATA="/data/db/postgres"
#PGDATA="/data/db/postgres/pgdata"
#PGDATA="/data/db/postgres-12/pgdata"

PGDATA="/data/db/postgres-11/pgdata"

(升级时,我倾向于保留注释掉的旧设置以供参考)

注意:配置文件未被编辑,每个路径都引用 ConfigDir(默认)

此外,对于 Pi 上的 Postgres,我设置:


random_page_cost = 1.1 
shared_buffers = 128MB
#work_mem = 4MB # keep the low default
effective_cache_size = 3GB # This is for a RaspberryPi-4
                           # for a Pi-3, I'd use ~700M

这就是 /etc/init.d/postgres 文件的样子::

#!/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 "" ]; then
    versions="       "
else
    get_versions
fi

case "" in
    start|stop|restart|reload)
        if [ "" = "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
             $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(, "down")) rc=3; printf ("%s/%s (port %s): %s\n", , , , )}; END {exit rc}'
        ;;
    force-reload)
        for v in $versions; do
            reload $v
        done
        ;;
    *)
        echo "Usage: [=10=] {start|stop|restart|reload|force-reload|status} [version ..]"
        exit 1
        ;;
esac

exit 0

好的,我想我已经弄明白了。可能有点矫枉过正,但它确实有效:

  1. 我做的第一件事是格式化并安装我的 1TB SSD。 Here 是一个很好的视频,用于演示格式化为 ext4 和安装。视频之间的区别在于我更新了 fstab 文件以在启动期间检查我的 SSD 或在 SSD 安装选项末尾检查“0 2”而不是“0 0”。

  2. 其次,我安装了Postgres。 Here 是一个很好的演练。该博客中提供的指导超出了我的需要,但仍然是一个很好的演练。我只是安装了 Postgres:

sudo apt install postgresql postgresql-contrib
  1. 第三,我遵循 this 演练直到第二步结束,但在开始第二步之前,我添加了一个符号 link 从 /var/lib/postgresql/12/main/YOUR/MOUNT/POSITION/postgresql/12/main执行:
ln -s /var/lib/postgresql/12/main /YOUR/MOUNT/POSITION/postgresql/12/main
  1. 最后,在重新启动 postgres 服务器之前,我使用 this 网站帮助我更好地配置我的服务器。输入您的规格,它应该会为您提供一些有用的配置设置。

如果我记得我遗漏了什么,我会试着回来编辑这个 post。否则,如果有任何不合理或不清楚的地方,请发表评论。