环境:
OS:CentOs 7
Postgres-11.2 源码编译
[root@localhost ~]# wget https://ftp.postgresql.org/pub/source/v11.2/postgresql-11.2.tar.gz --no-check-certificate
--2022-05-06 11:30:27-- https://ftp.postgresql.org/pub/source/v11.2/postgresql-11.2.tar.gz
正在解析主机 ftp.postgresql.org (ftp.postgresql.org)... 147.75.85.69, 72.32.157.246, 217.196.149.55, ...
正在连接 ftp.postgresql.org (ftp.postgresql.org)|147.75.85.69|:443... 已连接。
警告: 无法验证 ftp.postgresql.org 的由 “/C=US/O=Let's Encrypt/CN=R3” 颁发的证书:
颁发的证书已经过期。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:25779561 (25M) [application/octet-stream]
正在保存至: “postgresql-11.2.tar.gz”
100%[=======================>] 25,779,561 6.26MB/s 用时 5.0s
2022-05-06 11:30:33 (4.96 MB/s) - 已保存 “postgresql-11.2.tar.gz” [25779561/25779561])
[root@localhost ~]#
安装依赖包
yum -y install readline
yum -y install gcc
yum -y install -y readline-devel
yum -y install zlib-devel
编译安装
[root@localhost ]# tar -xvf postgresql-11.2.tar.gz
[root@localhost ]#mkdir -p /opt/postgresql-11.2
[root@localhost ]# cd postgresql-11.2
[root@localhost ]#./configure --prefix=/opt/postgresql-11.2
[root@localhost ]#make
[root@localhost ]#make install
创建相应的用户
[root@localhost opt]# groupadd postgres
[root@localhost opt]# useradd -g postgres postgres
创建数据及日志目录,并做相应授权
[root@localhost ]#mkdir -p /opt/postgresql-11.2/{data,log}
[root@localhost ]#chown -R postgres:postgres /opt/postgresql-11.2
初始化数据库
#su - postgres
[postgres@localhost bin]$ cd /opt/postgresql-11.2/bin
[postgres@localhost bin]$ ./initdb -D /opt/postgresql-11.2/data/
启动数据库
[postgres@localhost bin]$ cd /opt/postgresql-11.2/bin/
[postgres@localhost bin]$ ./pg_ctl -D /opt/postgresql-11.2/data/ -l /opt/postgresql-11.2/log/postgres.log start
waiting for server to start.... done
server started
修改环境变量
[postgres@localhost ~]$ vi .bash_profile
[postgres@localhost ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/opt/postgresql-11.2/bin
export PATH
登陆使用
[postgres@localhost ~]$ cd /opt/postgresql-11.2/bin/
[postgres@localhost bin]$ ./psql
psql (11.2)
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes | Mem
ber of
-----------+------------------------------------------------------------+----
-------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres=#
修改postgres用户的访问密码并测试建库建表
PostgreSQL 数据库默认会创建一个postgres的数据库用户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码,这里设定为1.
重启一下
[postgres@localhost bin]$ ./pg_ctl -D /opt/postgresql-11.2/data/ -l /opt/postgresql-11.2/log/postgres.log restart
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started
//进入命令行
[postgres@localhost bin]$ psql
psql (11.2)
Type "help" for help.
//修改密码
postgres=# ALTER USER postgres WITH PASSWORD '1';
ALTER ROLE
配置postgresql允许远程访问
只需要修改data目录下的pg_hba.conf和postgresql.conf这两个文件:
pg_hba.conf:配置对数据库的访问权限;
postgresql.conf:配置PostgreSQL数据库服务器的相应的参数
[root@master ~]# vi /opt/postgresql-11.2/data/pg_hba.conf
[root@master ~]# su - postgres
上一次登录:五 5月 6 10:29:00 CST 2022pts/1 上
[postgres@master ~]$ pg_ctl -D /opt/postgresql-11.2/data reload
server signaled
[postgres@master ~]$
修改postgresql.conf
vi /opt/postgresql-11.2/data/postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on; //去掉注释
修改该改参数需要重启动
[root@master ~]# su - postgres
上一次登录:五 5月 6 14:05:23 CST 2022pts/1 上
[postgres@master ~]$ pg_ctl -D /opt/postgresql-11.2/data -l /opt/postgresql-11.2/log/postgres.log stop
waiting for server to shut down.... done
server stopped
[postgres@master ~]$ pg_ctl -D /opt/postgresql-11.2/data -l /opt/postgresql-11.2/log/postgres.log start
waiting for server to start.... done
server started
参考文档1
参考文档2
创建同步账号
[postgres@master ~]$ psql
psql (11.2)
Type "help" for help.
postgres=# CREATE ROLE repl login replication encrypted password '1';
CREATE ROLE
// 修改配置文件(pg_hba.conf)
在该文件最后添加如下两行:
[root@master ~]# vi /opt/postgresql-11.2/data/pg_hba.conf
host replication repl 10.90.100.31/24 trust
host all repl 10.90.100.31/24 trust
// 修改配置文件(postgresql.conf)
[root@master ~]# vi /opt/postgresql-11.2/data/postgresql.conf
找到相应的参数进行如下配置修改
wal_level = replica
archive_mode = on
archive_command = 'cp %p /opt/postgresql-11.2/data/pg_archive/%f'
##%p = path of file to archive
##%f = file name only
max_wal_senders = 6
wal_keep_segments = 10240
wal_sender_timeout = 60s
创建归档日志目录
[root@master ~]# mkdir -p /opt/postgresql-11.2/data/pg_archive
重启主库
[postgres@master ~]$ pg_ctl -D /opt/postgresql-11.2/data -l /opt/postgresql-11.2/log/postgres.log restart
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started
从库的安装跟主库安装步骤一致,需要启动数据库
若从库的数据库已经在运行的话,事先将其停掉
[postgres@slave ~]$ pg_ctl -D /opt/postgresql-11.2/data -l /opt/postgresql-11.2/log/postgres.log stop
waiting for server to shut down.... done
server stopped
从库安装完成后,不初始化,若已经初始化,删除其data目录
若之前安装的pg有data目录的话需要将其删除掉,并创建一个空的相同的目录
[root@slave ~]# su - postgres
[postgres@slave ~]$ cd /opt/postgresql-11.2
[postgres@slave postgresql-11.2]$ mv data bakdata
[postgres@slave postgresql-11.2]$ mkdir data
[root@slave ~]# chown -R postgres:postgres /opt/postgresql-11.2
[root@slave ~]# chmod 0700 /opt/postgresql-11.2/data
[postgres@slave postgresql-11.2]$ pg_basebackup -Fp --progress -D /opt/postgresql-11.2/data -h 10.90.100.31 -p 5432 -U repl --password
Password:
23780/23780 kB (100%), 1/1 tablespace
(注:若执行pg_basebackup显示为下,可以关闭防火墙重新尝试
[postgres@slave postgresql-11.2]$ pg_basebackup -Fp --progress -D /opt/postgresql-11.2/data -h 10.90.100.31 -p 5432 -U repl --password
Password:
pg_basebackup: could not connect to server: could not connect to server: 没有到主机的路由
Is the server running on host "10.90.100.31" and accepting
TCP/IP connections on port 5432?
)
// 可以看到data目录下的所有文件都同步过来了
[postgres@slave postgresql-11.2]$ ls -al /opt/postgresql-11.2/data/
总用量 56
drwx------. 20 postgres postgres 4096 5月 24 10:54 .
drwxr-xr-x. 9 postgres postgres 94 5月 24 10:05 ..
-rw-------. 1 postgres postgres 224 5月 24 10:54 backup_label
drwx------. 5 postgres postgres 41 5月 24 10:54 base
drwx------. 2 postgres postgres 4096 5月 24 10:54 global
drwxr-xr-x. 2 postgres postgres 6 5月 24 10:54 pg_archive
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_commit_ts
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_dynshmem
-rw-------. 1 postgres postgres 4723 5月 24 10:54 pg_hba.conf
-rw-------. 1 postgres postgres 1636 5月 24 10:54 pg_ident.conf
drwx------. 4 postgres postgres 68 5月 24 10:54 pg_logical
drwx------. 4 postgres postgres 36 5月 24 10:54 pg_multixact
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_notify
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_replslot
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_serial
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_snapshots
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_stat
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_stat_tmp
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_subtrans
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_tblspc
drwx------. 2 postgres postgres 6 5月 24 10:54 pg_twophase
-rw-------. 1 postgres postgres 3 5月 24 10:54 PG_VERSION
drwx------. 3 postgres postgres 60 5月 24 10:54 pg_wal
drwx------. 2 postgres postgres 18 5月 24 10:54 pg_xact
-rw-------. 1 postgres postgres 88 5月 24 10:54 postgresql.auto.conf
-rw-------. 1 postgres postgres 23891 5月 24 10:54 postgresql.conf
从模板文件拷贝到data目录
[postgres@slave ~]$ cp /opt/postgresql-11.2/share/recovery.conf.sample /opt/postgresql-11.2/data/recovery.conf
[postgres@slave data]$ pwd
/opt/postgresql-11.2/data
// 对其取消注释进行修改,参数如下:
在data目录下创建recovery.conf文件,内容如下:
standby_mode = on # 这个说明这台机器为从库
primary_conninfo = 'host=10.90.100.31 port=5432 user=repl password=1' # 这个说明这台机器对应主库的信息
recovery_target_timeline = 'latest' # 这个说明这个流复制同步到最新的数据
修改如下内容项:
max_connections = 1000 #一般查多于写的应用从库的最大连接数要比较大
hot_standby = on #说明这台机器不仅仅是用于数据归档,也用于数据查询
max_standby_streaming_delay = 30s #数据流备份的最大延迟时间
wal_receiver_status_interval = 10s #多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
hot_standby_feedback = on #如果有错误的数据复制,是否向主进行反馈
[postgres@slave data]$ pg_ctl -D /opt/postgresql-11.2/data/ -l /opt/postgresql-11.2/log/postgres.log start
waiting for server to start.... done
server started
//
主数据库操作
[postgres@master ~]$ psql
psql (11.2)
Type "help" for help.
postgres=# select * from pg_replication_slots;
slot_name | plugin | slot_type | datoid | database | temporary | active | activ
xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn
-----------+--------+-----------+--------+----------+-----------+--------+------
------+--------------+-------------+---------------------
(0 rows)
postgres=# create database db1;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privile
ges
-----------+----------+----------+-------------+-------------+-----------------
------
db1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/pos
tgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/pos
tgres
(4 rows)
postgres=# \c db1
You are now connected to database "db1" as user "postgres".
db1=# \d
Did not find any relations.
db1=# create table t1 (id int,name varchar(20));
CREATE TABLE
db1=# insert into t1 values (1,'yaya');
INSERT 0 1
db1=# insert into t1 values (1,'hh');
INSERT 0 1
db1=# select * from t1;
id | name
----+------
1 | yaya
1 | hh
(2 rows)
db1=#
// 从数据库操作
[postgres@slave data]$ psql
psql (11.2)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
db1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# \c db1
You are now connected to database "db1" as user "postgres".
db1=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | t1 | table | postgres
(1 row)
db1=# select * from t1;
id | name
----+------
1 | yaya
1 | hh
(2 rows)
db1=#