安装环境:CentOS x64 安装 postgresql-12.3

一、下载源码

https://www.postgresql.org/ftp/source/v12.3/

二、安装依赖

snippet.bash
yum install -y perl-ExtUtils-Embed
yum install -y readline-devel
yum install -y zlib-devel
yum install -y pam-devel
yum install -y libxml2-devel
yum install -y libxslt-devel
yum install -y openldap-devel
yum install -y python-devel
yum install -y gcc-c++
yum install -y openssl-devel
yum install -y cmake

三、编译与安装

snippet.bash
tar xf postgresql-12.3.tar.gz
cd postgresql-12.3/
./configure --prefix=/opt/postgresql
make -j4
make install

./configure 可选参数:

安装后检查安装目录

snippet.bash
[root@bogon postgresql-12.3]# cd /opt/postgresql/
[root@bogon postgresql]# ll
总用量 16
drwxr-xr-x. 2 root root 4096 624 15:38 bin
drwxr-xr-x. 6 root root 4096 624 15:38 include
drwxr-xr-x. 4 root root 4096 624 15:38 lib
drwxr-xr-x. 6 root root 4096 624 15:38 share

编译debug版本

如果需要编译debug版本,可以这样configure:

./configure --enable-cassert --enable-debug --enable-dtrace --prefix=/opt/postgresql CFLAGS="-ggdb -O0"

四、创建postgres用户组与用户

snippet.bash
groupadd postgres
useradd -g postgres postgres

五、配置postgres环境

snippet.bash
[root@bogon postgresql]# su - postgres
[postgres@bogon ~]$ vim ~/.bash_profile
# 添加:
export PGHOME=/opt/postgresql
export PGDATA=/opt/pgdata
PATH=$PGHOME/bin:$PATH:$HOME/bin
 
[postgres@bogon ~]$ source .bash_profile

六、准备数据库目录

snippet.bash
[postgres@bogon ~]$ mkdir /opt/pgdata

七、初始化数据库

snippet.bash
[postgres@bogon ~]$ initdb /opt/pgdata
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
……
Success. You can now start the database server using:
 
pg_ctl -D /opt/pgdata -l logfile start

八、启停服务

snippet.bash
#启动服务
[postgres@bogon ~]$ pg_ctl -D /opt/pgdata -l ~/logfile start
 
#停止服务
[postgres@bogon ~]$ pg_ctl -D /opt/pgdata -l ~/logfile stop

九、开机自启动

PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。

linux文件即为linux系统上的启动脚本

snippet.bash
[postgres@localhost ~]$ cd /opt/postgresql-12.3/contrib/start-scripts
[postgres@localhost start-scripts]$ ls
freebsd  linux  macos

切换为root用户,修改linux文件属性,添加X属性

snippet.bash
[root@localhost start-scripts]# chmod a+x linux

复制linux文件到/etc/init.d目录下,更名为postgresql

snippet.bash
[root@localhost start-scripts]# cp linux /etc/init.d/postgresql

修改/etc/init.d/postgresql文件的两个变量

prefix设置为postgresql的安装路径:/pgsql/postgresql

PGDATA设置为postgresql的数据目录路径:/pgsql/postgresql/data

设置postgresql服务开机自启动

snippet.bash
[root@localhost init.d]# chkconfig --add postgresql

查看开机自启动服务设置成功。

snippet.bash
[root@localhost init.d]# chkconfig
postgresql         0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭

5)编辑/etc/sysconfig/iptables文件开放5432端口。

snippet.bash
[root@localhost sysconfig]# cd /etc/sysconfig
[root@localhost sysconfig]# vi iptables
# 添加以下内容
-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT

重启服务

snippet.bash
[root@localhost sysconfig]# /etc/init.d/iptables restart

查看端口是否开放

snippet.bash
[root@localhost sysconfig]# /sbin/iptables -L -n

执行service postgresql start,启动PostgreSQL服务

snippet.bash
[root@localhost init.d]# service postgresql start