Postgresql 服务器无法启动

mik*_*927 17 postgresql

[Ubuntu 16.04] 我安装了 postgresql 9.5 以及依赖项:

sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.5 libpq-dev
Run Code Online (Sandbox Code Playgroud)

当我想跑步时,psql我得到:

psql: 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"?
Run Code Online (Sandbox Code Playgroud)

但是/var/run/postgresql/是空的。当我重新启动 posgresql 时,一切似乎都很好:

$ /etc/init.d/postgresql restart
[ ok ] Restarting postgresql (via systemctl): postgresql.service.

$ /etc/init.d/postgresql status
? postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (exited) since wto 2016-09-27 16:18:26 CEST; 1min 15s ago
  Process: 3076 ExecReload=/bin/true (code=exited, status=0/SUCCESS)
  Process: 3523 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 3523 (code=exited, status=0/SUCCESS)
Run Code Online (Sandbox Code Playgroud)

但是如果检查ps aux没有这样的PID(为什么??)

完全重新安装根本没有帮助。我该如何解决?

Til*_*man 18

这是 Xenial 中 PostgreSQL 系统集成的一个特性。

postgresql-common 包安装的 postgresql 服务单元只是一个虚拟服务,它导致实际服务 postgresql@9.6-main 通过依赖项启动。您可以通过运行命令来查看该依赖项

systemctl list-dependencies postgresql
Run Code Online (Sandbox Code Playgroud)

该依赖项不是永久性的,而是在系统启动期间由 systemd 生成器生成的/lib/systemd/system-generators/postgresql-generator,该生成器也随 postgresql-common 包一起提供。生成器检查文件中的启动模式/etc/postgresql/9.6/main/start.conf是否设置为auto,如果是,则设置随后导致实例 9.6-main 启动的依赖项。

(更准确地说,它会检查所有配置子目录/etc/postgresql/*/*,并将为所有配置为自动启动的实例创建依赖项,但在默认安装中只会有一个实例。)

由于 systemd 生成器的限制(请参阅 参考资料man systemd.generator),此过程可能会失败,导致重新启动后依赖项不存在。Systemd 然后将启动虚拟服务,写入

systemd[1]: Starting PostgreSQL RDBMS...
systemd[1]: Started PostgreSQL RDBMS.
Run Code Online (Sandbox Code Playgroud)

到日志,但除此之外什么都不做。尝试通过以下方式手动启动服务

systemctl start postgresql
Run Code Online (Sandbox Code Playgroud)

只会重现那个结果。运行命令

systemctl daemon-reload
Run Code Online (Sandbox Code Playgroud)

手动作为 root 将重新运行生成器,并且在大多数情况下修复问题,直到下一次重新启动。

要永久解决此问题,您必须找出发电机在启动期间出现故障的原因。可能的原因可以在 systemd.generator 联机帮助页中找到。在我的例子中,它是 PostgreSQL 配置文件/etc/postgresql/9.6/main/postgresql.conf,它被符号链接到一个不同的文件系统,当生成器在启动过程中早期运行时,该文件系统尚不可用。postgresql-generator检查该文件是否存在,即使它不需要它。


小智 10

扩展了蒂尔曼的回答,但没有足够的荣誉来评论......

如果您不需要将服务称为 postgresql 并且不关心包装器虚拟服务,那么它应该可以直接控制真正的服务。它的名字是:postgresql@$version-$cluster.service在你的情况下,它应该是postgresql-9.5-main简而言之。喜欢开始

systemctl start postgresql@9.5-main
Run Code Online (Sandbox Code Playgroud)

并停止:

systemctl stop postgresql@9.5-main
Run Code Online (Sandbox Code Playgroud)

Status还会为您提供比自动生成的包装器服务更好、更准确的信息。

systemctl status postgresql@9.5-main
Run Code Online (Sandbox Code Playgroud)

对于 9.6,它看起来像这样:

? postgresql@9.6-main.service - PostgreSQL Cluster 9.6-main
   Loaded: loaded (/lib/systemd/system/postgresql@.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2017-09-13 00:41:50 CEST; 7h ago
  Process: 10235 ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast %i stop (code=exited, status=2)
  Process: 10676 ExecStart=postgresql@%i --skip-systemctl-redirect %i start (code=exited, status=0/SUCCESS)
 Main PID: 10683 (postgres)
   CGroup: /system.slice/system-postgresql.slice/postgresql@9.6-main.service
           ??10683 /usr/lib/postgresql/9.6/bin/postgres -D /var/lib/postgresql/9.6/main -c config_file=/etc/postgresql/9.6/main/postgresql.conf
           ??10685 postgres: 9.6/main: checkpointer process
           ??10686 postgres: 9.6/main: writer process
           ??10748 postgres: 9.6/main: wal writer process
           ??10749 postgres: 9.6/main: autovacuum launcher process
           ??10750 postgres: 9.6/main: archiver process   last was 000000020000000000000082
           ??10751 postgres: 9.6/main: stats collector process
Run Code Online (Sandbox Code Playgroud)


Flo*_*ker 4

就我而言,这与错误配置的区域设置有关。

我在 dba.stackexchange.com 答案中找到了解决方案:

  1. 用于sudo dpkg-reconfigure locales生成必要的语言环境
  2. 通过删除现有的数据库集群sudo pg_dropcluster 9.5 main(这将删除集群中的所有数据!)
  3. 通过重新创建集群sudo pg_createcluster 9.5 main --start
  4. 通过以下方式重新启动 PostgreSQLsudo service postgresql restart


Ami*_*ias 1

最好在 ubuntu 16.04 中使用 systemd 启动脚本,这些天初始化脚本可能无法正常工作。Postgres 9.5 已经在 ubuntu 存储库中,所以尝试一下,它应该有 systemd 启动。