(1)下载软件包
https://www.postgresql.org/ftp/source/v12.7/
(2)安装必要包
yum -y install readline readline-devel zlib zlib-devel gettext gettext-devel openssl openssl-devel pam pam-devel libxml2 libxml2-devel libxslt libxslt-devel perl perl-devel tcl-devel uuid-devel gcc gcc-c++ make flex bison perl-ExtUtils*
(3)创建用户
groupadd dba -g 2000
useradd postgres -g 2000 -u 2000
id postgres
echo “postgres123”|passwd --stdin postgres
(4)配置目录和权限
mkdir -p /opt/soft
mkdir -p /opt/pg12
mkdir -p /opt/pgdata
mkdir -p /opt/pgwal
mkdir -p /opt/pgarchive
chown -R postgres:dba /opt
chmod 0700 /opt/pgdata/
chmod 0700 /opt/pgwal/
chmod 0700 /opt/pgarchive/
(5)解压
mv postgresql-12.7.tar.gz /opt/soft
su - postgres
cd /opt/soft
tar -xzvf postgresql-12.7.tar.gz
(6)编译安装
su - postgres
cd /opt/soft
cd postgresql-12.7/
./configure --prefix=/opt/pg12 --with-pgport=5432
gmake world
gmake install-world
(7)初始化和启动
cd /opt
ln -s /opt/pg12 /opt/pgsql
初始化:输入密码123456
/opt/pgsql/bin/initdb -D /opt/pgdata/ -X /opt/pgwal/ -E UTF8 -Upostgres -W
启动
/opt/pgsql/bin/pg_ctl -D /opt/pgdata/ -l logfile start
查看版本
/opt/pg12/bin/postgres --version
postgres (PostgreSQL) 12.7
(8)配置环境变量
vi ~/.bash_profile
export PGPORT=5432
export PGUSER=postgres
export PGHOME=/opt/pgsql
export PGDATA=/opt/pgdata
export PATH=
P
G
H
O
M
E
/
b
i
n
:
PGHOME/bin:
PGHOME/bin:PATH
source ~/.bash_profile
(9)数据库启停
查看数据库运行状态
pg_ctl -D /opt/pgdata status
启动数据库
pg_ctl -D /opt/pgdata start &
/opt/pgsql/bin/pg_ctl -D /opt/pgdata/ -l logfile start
查看数据库运行状态
pg_ctl -D /opt/pgdata status
停止数据库
pg_ctl -D /opt/pgdata stop
postgres=# select version();
重启数据库
pg_ctl -D /opt/pgdata restart &