由普通用户安装 PostgreSQL 10。
启用并启动其服务:
sudo systemctl start postgresql-10
sudo systemctl enable postgresql-10
Run Code Online (Sandbox Code Playgroud)
然后使用用户登录postgres:
sudo su - postgres
Run Code Online (Sandbox Code Playgroud)
将此设置添加到.bash_profile文件中:
export PATH=$PATH:/usr/pgsql-10/bin/
Run Code Online (Sandbox Code Playgroud)
通过以下方式重新启动 PostgreSQL pg_ctl:
-bash-4.2$ pg_ctl restart
Run Code Online (Sandbox Code Playgroud)
然后退出到普通用户,再次检查postgresql的状态,失败。即使用这种方式重启也不能成功:
sudo systemctl restart postgresql-10
sudo systemctl stop postgresql-10
Run Code Online (Sandbox Code Playgroud)
总是如此failed。
但如果我使用postgres用户来测试重新启动、停止、启动,它们都可以成功。
这两种方式控制的不是同一个过程?
添加postgresql-10服务内容
$ sudo systemctl cat postgresql-10.service
# /usr/lib/systemd/system/postgresql-10.service
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades. If you want to customize, the
# best way is to create a file "/etc/systemd/system/postgresql-10.service",
# containing
# .include /usr/lib/systemd/system/postgresql-10.service
# ...make your changes here...
# For more info about custom unit files, see
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F
# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.
# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-setup.
[Unit]
Description=PostgreSQL 10 database server
Documentation=https://www.postgresql.org/docs/10/static/
After=syslog.target
After=network.target
[Service]
Type=notify
User=postgres
Group=postgres
# Note: avoid inserting whitespace in these Environment= lines, or you may
# break postgresql-setup.
# Location of database directory
Environment=PGDATA=/var/lib/pgsql/10/data/
# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0
ExecStartPre=/usr/pgsql-10/bin/postgresql-10-check-db-dir ${PGDATA}
ExecStart=/usr/pgsql-10/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
# Do not set any timeout value, so that systemd will not kill postmaster
# during crash recovery.
TimeoutSec=0
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
如果您使用手动启动服务器pg_ctl,systemd则不知道。
它会很高兴地尝试启动服务器,但这会失败(因为服务器已经在运行),并且它无法停止服务器,因为它不知道它正在运行。
因此,请务必systemctl在启动和停止服务时使用。重新加载pg_ctl reload就好了。