Ubuntu Postgresql开机自启动服务

 1. 建立service文件

sudo vim /etc/systemd/system/postgresql.service
2. postgresql service文件
[Unit]
Description=PostgreSQL 14 database server
Documentation=man:postgres(1)
Documentation=http://www.postgresql.org/docs/14/static/
After=network.target
 
[Service]
Type=forking
User=postgres
Group=postgres
ExecStart=/usr/bin/pg_ctl start -D /usr/local/postgresql-11.4/data -l /usr/local/postgresql-11.4/log/pg_server.log 
ExecStop=/usr/bin/pg_ctl stop -D /usr/local/postgresql-11.4/data -l /usr/local/postgresql-11.4/log/pg_server.log
ExecReload=/usr/bin/pg_ctl reload -D /usr/local/postgresql-11.4/data -l /usr/local/postgresql-11.4/log/pg_server.log
PIDFile=/usr/local/postgresql-11.4/data/postmaster.pid
TimeoutStopSec=0
Restart=always
User=postgres
Group=postgres
RuntimeDirectory=postgresql
RuntimeDirectoryMode=2755
UMask=007
PrivateTmp=yes
LimitNOFILE=65535
PrivateDevices=yes
ProtectHome=yes
ReadOnlyDirectories=/
ReadWriteDirectories=-/usr/local/postgresql-11.4
ReadWriteDirectories=-/etc/postgresql

[Install]
WantedBy=multi-user.target
Alias=postgresql.service
3. 启动服务并检查状态
$ systemctl daemon-reload
$ systemctl start postgresql
$ systemctl status postgresql
$ systemctl restart postgresql

4. 开机自启动 

$ systemctl enable postgresql

你可能感兴趣的:(ubuntu,postgresql,linux)