CentOS 编译安装 PostgreSQL 11 开机自动启动 (2206 views)

gHOST

2019-06-02 21:54:40

使用 yum 安装编译环境

[root@localhost ~]# yum groupinstall -y "Development tools"
[root@localhost ~]# yum install -y bison flex readline-devel zlib-devel

下载源码、解压

[root@localhost ~]# wget https://ftp.postgresql.org/pub/source/v11.5/postgresql-11.5.tar.bz2
[root@localhost ~]# tar -xjvf postgresql-11.5.tar.bz2

配置、编译

使用用默认配置,安装目录为 /usr/local/pgsql,端口:5432

[root@localhost ~]# cd postgresql-11.5
[root@localhost postgresql-11.5]# ./configure
[root@localhost postgresql-11.5]# gmake world && gmake install

将 PostgreSQL 程序目录加入环境变量

编辑 /etc/profile 文件,在末尾加上

export PATH=/usr/local/pgsql/bin:$PATH

保存,退出;运行以下名称,使环境变量生效

[root@localhost postgresql-11.5]# source /etc/profile

创建 postgres 用户

[root@localhost postgresql-11.5]# useradd postgres

创建数据库目录,并修改相应权限

[root@localhost postgresql-11.5]# mkdir -p /data/pgsql/{data,backups,scripts,archive_wals}
[root@localhost postgresql-11.5]# chown -R postgres:postgres /data/pgsql
[root@localhost postgresql-11.5]# chmod 0700 -R /data/pgsql

切换到 postgres 用户,初始化数据库目录,并输入数据库超级用户密码

[root@localhost postgresql-11.5]# su postgres
[postgres@localhost postgresql-11.5]$ initdb -D /data/pgsql/data -W
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

Enter new superuser password: 
Enter it again: 

fixing permissions on existing directory /data/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /data/pgsql/data -l logfile start

[postgres@localhost postgresql-11.5]$ exit
exit
[root@localhost postgresql-11.5]# 

创建 postgresql.service 服务脚本

[root@localhost postgresql-11.5]# vi /usr/lib/systemd/system/postgresql.service

输入以下内容

[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
Type=forking

User=postgres
Group=postgres

# Port number for server to listen on
Environment=PGPORT=5432

# Location of database directory
Environment=PGDATA=/data/pgsql/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

#ExecStartPre=/usr/local/pgsql/bin/postgresql-check-db-dir ${PGDATA}
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

[Install]
WantedBy=multi-user.target

设置权限

[root@localhost postgresql-11.5]# chmod 754 /usr/lib/systemd/system/postgresql.service

设置为开机自启动:

[root@localhost postgresql-11.5]# systemctl enable postgresql.service

常用指令

启动服务

[root@localhost postgresql-11.5]# systemctl  start  postgresql.service

停止服务

[root@localhost postgresql-11.5]# systemctl stop postgresql.service

重启服务

[root@localhost postgresql-11.5]# systemctl restart postgresql.service
[root@localhost postgresql-11.5]# service postgresql restart

使服务自动启动

[root@localhost postgresql-11.5]# systemctl enable postgresql.service

使服务不自动启动

[root@localhost postgresql-11.5]# systemctl disable postgresql.service

检查服务状态

[root@localhost postgresql-11.5]# systemctl   status  postgresql.service
[root@localhost postgresql-11.5]# systemctl   is-active postgresql.service

显示所有已启动的服务

[root@localhost postgresql-11.5]# systemctl   list-units --type=service