Centos 7.x 配置PostgreSQL自启动

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB PostgreSQL 版,企业版 4核16GB
推荐场景:
HTAP混合负载
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介: PostgreSQL starts with the systemThe document describe how to configure automatic start PostgreSQL when the system starts.

PostgreSQL starts with the system

The document describe how to configure automatic start PostgreSQL when the system starts.

The ways suitable for  systemd  on CentOS 7.0 or latest release.


1.  Create and configure postgresql.service

# vim /usr/lib/systemd/system/postgresql.service

[Unit]  

    Description=PostgreSQL database server

    After=remote-fs.target nss-lookup.target  

    After=network.target sshd.service

    After=proc-fs-nfsd.mount

    After=network.target local-fs.target

    After=nfs-config.service

    After=nfs-mountd.service

    After=nfs-blkmap.service

    After=nfs-client.target

    After=nfs-config.service

    After=nfs-idmapd.service

    After=nfs-idmap.service

    After=nfs-lock.service

    After=nfslock.service

    After=nfs-mountd.service

    After=nfs-secure-server.service

    After=nfs-secure.service

    After=nfs.service

    After=nfs-utils.service

[Service]  

    Type=forking  

    User=postgres

    Group=appuser

    Environment=PGPORT=5432  

    Environment=PGDATA=/data/01/local/pgsql/data  

    ExecStart=/data/01/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300  

    ExecStop=/data/01/local/pqsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast  

    ExecReload=/data/01/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s  

    TimeoutSec=300  

[Install]  

    WantedBy=remote-fs.target

#chmod 644 /usr/lib/systemd/system/postgresql.service


2.  Reload systemd  and enable postgresql.service

#systemctl daemon-reload


#systemctl enable postgresql.service

Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql.service to /usr/lib/systemd/system/postgresql.service.


#systemctl is-enabled postgresql.service

Enabled


#systemctl start postgresql.service


#systemctl status postgresql.service

● postgresql.service - PostgreSQL database server

   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)

   Active: active (running) since Fri 2018-02-23 22:48:49 KST; 28min ago

Main PID: 2506 (postgres)

   CGroup: /system.slice/postgresql.service

           ├─2506 /data/01/local/pgsql/bin/postgres -D /data/01/local/pgsql/data -p 5432

           ├─2508 postgres: logger process   

           ├─2510 postgres: checkpointer process   

           ├─2511 postgres: writer process   

           ├─2512 postgres: wal writer process   

           ├─2513 postgres: autovacuum launcher process   

           └─2514 postgres: stats collector process   

Feb 23 22:48:47 ec5d-pbfcompilation-02 systemd[1]: Starting PostgreSQL database server...

Feb 23 22:48:48 ec5d-pbfcompilation-02 pg_ctl[1414]: < 2018-02-23 22:48:48.671 KST >LOG:  redirecting log output to logging collector process

Feb 23 22:48:48 ec5d-pbfcompilation-02 pg_ctl[1414]: < 2018-02-23 22:48:48.671 KST >HINT:  Future log output will appear in directory "pg_log".

Feb 23 22:48:49 ec5d-pbfcompilation-02 systemd[1]: Started PostgreSQL database server.


Note: CentOS7.x not support /etc/rc.local script file, Detailed description as following:

#cat /etc/rc.local

#!/bin/bash

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES

#

# It is highly advisable to create own systemd services or udev rules

# to run scripts during boot instead of using this file.

#这是明智的在系统重启的时候,去创建一个systemd 服务或者udev规则去运行脚本,而不是用/etc/rc.local

#

# In contrast to previous versions due to parallel execution during boot

# this script will NOT be run after all other services.

#与先前的系统版本相比较,在系统启动的时候,由于服务是并行运行的,所以在其他服务启动之后,/etc/rc.local不会运行

#

# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure

# that this script will be executed during boot.

虽然这里说可以使用chmod  +x /etc/rc.local 去执行这个脚本,但是笔者在这个文件中添加一行:su - postgres -c ‘pg_ctl start -D /usr/local/pgsql/data’

然后reboot测试了多次,postgres最后都没有随系统启动成功,所以不再推荐(官方也不推荐了)使用/etc/rc.local文件的方式启动postgres,启动其他服务也一样,最好配置一个systemd服务。




相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
SQL 关系型数据库 Linux
【PostgreSQL】基于CentOS系统安装PostgreSQL数据库
【PostgreSQL】基于CentOS系统安装PostgreSQL数据库
658 0
|
存储 Oracle 关系型数据库
「PostgreSQL 」如何在CentOS 7 / CentOS 8上安装PostgreSQL 12
「PostgreSQL 」如何在CentOS 7 / CentOS 8上安装PostgreSQL 12
|
网络协议 关系型数据库 Linux
Centos7 PostgreSQL安装
Centos7 PostgreSQL安装
233 0
|
SQL 关系型数据库 MySQL
Mysql的安装与配置(Centos7)
Mysql的安装与配置(Centos7)
139 1
Mysql的安装与配置(Centos7)
|
关系型数据库 数据库 数据安全/隐私保护
CentOS7.5安装配置PostgreSQL10
CentOS7.5安装配置PostgreSQL10
265 0
CentOS7.5安装配置PostgreSQL10
|
关系型数据库 数据库 数据安全/隐私保护
CentOS7.5安装配置PostgreSQL11
CentOS7.5安装配置PostgreSQL11
257 0
CentOS7.5安装配置PostgreSQL11
|
关系型数据库 数据库 开发工具
CentOS7.X安装postgresql-10.3
CentOS7.X安装postgresql-10.3
2513 0
|
关系型数据库 Linux PostgreSQL
|
关系型数据库 MySQL Linux
CentOS 7 下使用yum安装MySQL5.7.20,并设置开启启动
CentOS 7 下使用yum安装MySQL5.7.20,并设置开启启动目录yum源中MySQL的安装 查看MySQL是否启动 查看MySQL相关的安装包 下载mysql的repo源,这源下载的是最新的版本 启动MySQL服务 查看MySQL初始密码并登录MySQL设置MySQL自启动 在开启...
11427 0
|
关系型数据库 MySQL 数据安全/隐私保护
centos7 部署和配置Mysql5
centos7安装和配置mysql5.7的步骤
1216 0