1、安装和启用EPEL、CRB

dnf config-manager --set-enabled crb
dnf -y install epel-release epel-next-release

  备注:若提示没有config-manager命令,请执行命令:

dnf install 'dnf-command(config-manager)' -y

2、安装PostgreSQL

dnf module -y install postgresql:15

3、初始化数据库

postgresql-setup --initdb

4、配置文件修改

# vim /var/lib/pgsql/data/postgresql.conf
listen_addresses = '*'    #第60行
max_connections = 1000    #第65行

# vim /var/lib/pgsql/data/pg_hba.conf
host    all             all             10.32.161.0/24          scram-sha-256    #最后一行增加

5、启动服务

systemctl enable --now postgresql

6、用户和数据库管理

# 切换到postgres用户
su - postgres
 
# 创建用户
createuser cent
 
# 配置用户密码
psql -c "alter user cent with password 'password';"
 
# 创建数据库
createdb testdb -O cent
 
# 验证用户连接数据库权限
psql -h 10.32.161.131 -d testdb -U cent
 
# 查看用户
psql -c "select usename from pg_user;"
 
# 查看数据库
psql -l
 
# 连接数据库
psql testdb
testdb=> \du     #查看用户角色
testdb=> \dt     #查看数据库表
testdb=> \q      #退出
 
# 删除数据库
dropdb testdb

7、备份与恢复

# 切换到postgres用户
su - postgres

# 通过指定用户备份指定数据库
pg_dump -U cent --format=t -d testdb > pg_testdb.tar

# 通过管理员备份所有数据库
pg_dumpall -f /backups/pg_DB_all.sql

# 通过指定用户恢复备份
pg_restore -U cent -d testdb pg_testdb.tar

# 通过管理员恢复备份
psql -f /backups/pg_DB_all.sql

8、主从流式复制部署

  1)环境

服务器名称 IP地址 备注
pg-161-131 10.32.161.131
pg-161-132 10.32.161.132

  2)主节点配置

# vim /var/lib/pgsql/data/postgresql.conf
...
listen_addresses = '*'
max_connections = 1000
wal_level = replica
synchronous_commit = on
max_wal_senders = 10
synchronous_standby_names = '*'
...

# vim /var/lib/pgsql/data/pg_hba.conf
...
#host    replication     all             127.0.0.1/32            ident    #注释掉
#host    replication     all             ::1/128                 ident    #注释掉
host    all             all             10.32.161.0/24          scram-sha-256
host    replication     rep_user        10.32.161.131/32        scram-sha-256    #增加
host    replication     rep_user        10.32.161.132/32        scram-sha-256    #增加

# 创建复制用户
# su - postgres
$ createuser --replication -P rep_user
Enter password for new role:     #输入密码
Enter it again:     #再次输入密码

# 重启服务
systemctl restart postgresql

  3)从节点配置

# 停止服务
systemctl stop postgresql

# 删除数据
rm -rf /var/lib/pgsql/data/*

# 从主节点获取数据
# su - postgres
$ pg_basebackup -R -h 10.32.161.131 -U rep_user -D /var/lib/pgsql/data -P
Password:     #输入复制用户的密码
23088/23088 kB (100%), 1/1 tablespace

# 修改配置
# vi /var/lib/pgsql/data/postgresql.conf
...
listen_addresses = '*'    #第60行
hot_standby = on    #第335行
...

# 启动服务
systemctl start postgresql

  4)登录主节点查看复制状态

# su - postgres
$ psql -c "select usename, application_name, client_addr, state, sync_priority, sync_state from pg_stat_replication;"
 usename  | application_name |  client_addr  |   state   | sync_priority | sync_state 
----------+------------------+---------------+-----------+---------------+------------
 rep_user | walreceiver      | 10.32.161.132 | streaming |             1 | sync
(1 row)

  

posted on 2023-04-13 10:06  a120608yby  阅读(230)  评论(0编辑  收藏  举报