一、安装依赖包

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

二、创建用户和数据目录

useradd postgres

mkdir postgres/data
mkdir postgres/logs

chown -R postgres:postgres postgres

三、数据库安装

./configure --prefix=/data1/pgstgres --with-python --with-perl

make && make install

四、 配置PostgreSQL环境变量

vi /etc/profile

export PATH=/data1/postgres/bin:$PATH
export PGDATA=/data1/postgres/data
export PGHOME=/data1/postgres
export LD_LIBRARY_PATH=/data1/postgres/lib
export LANG=zh_CN.UTF-8


source /etc/profile

五、初始化数据库

su - postgres

initdb -D /data1/postgres/data --locale=zh_CN.UTF8

# 启动数据库
pg_ctl start

# 停止数据库
pg_ctl stop

# 重启数据库
pg_ctl restart -m fast

六、配置文件修改参数

  • vi /data1/postgres/data/postgresql.conf
  • 配置 日志和 监听地址
## 修改 监听ip 地址端口
listen_addresses = '0.0.0.0'

## 修改 日志 路径(默认不生成日志为文件)
log_destination = 'csvlog'
logging_collector = on
log_directory = '/data1/postgres/logs'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_rotation_age = 1d
log_rotation_size = 100MB
log_min_messages = info
# 记录执行慢的SQL
log_min_duration_statement = 60
log_checkpoints = on
log_connections = on
log_disconnections = on
log_duration = on
log_line_prefix = '%m'
# 监控数据库中长时间的锁
log_lock_waits = on
# 记录DDL操作
#log_statement = 'ddl'

  • 配置 数据库 密码
## 进入数据库 psql ,设置密码

postgres=# ALTER USER postgres WITH PASSWORD '123456';
ALTER ROLE
postgres=# select * from pg_shadow ;
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |               passwd                | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+-------------------------------------+----------+-----------
 test     |    16384 | f           | f        | f       | f            | md55a2e54ee57e5b7273b9a8fed78c1ebd8 |          |
 postgres |       10 | t           | t        | t       | t            | md5a3556571e93b0d20722ba62be61e8c2d |          |
(2 rows)

  • 配置数据支持密码远程连接
  • vi /data1/postgres/data/pg_hba.conf
#添加 支持的 ip 段
host    all             all             192.168.30.0/24            md5