一. 源码安装PostgreSQL12.5
主机环境:centos7.9.2009
主机地址:192.168.5.130
#安装包下载地址
https://ftp.postgresql.org/pub/source/v12.5/postgresql-12.5.tar.gz
#解压缩
tar -zxf postgresql-12.5.tar.gz
#安装依赖包
yum install -y readline-devel
#编译
cd postgresql
./configure -prefix /usr/local/pgsql-12
make & make install # (执行时间5-10分钟)
#创建数据目录并授权
mkdir -p /pgdata6a/pg_sjcszj/data && chown -R postgres. /pgdata6a/pg_sjcszj/data
#切换到postgres 账户并初始化
su - postgres
/usr/local/pgsql-12/bin/initdb -D /pgdata6a/pg_sjcszj/data --wal-segsize=64
#启动数据库,登录并修改密码
/usr/local/pgsql-12/bin/pg_ctl -D /pgdata6a/pg_sjcszj/data -l logfile start
/usr/local/pgsql-12/bin/psql -p 54321
alter user postgres with password 'postgres';
#刷新配置文件
su - postgres
/usr/local/pgsql-12/bin/pg_ctl reload -D /pgdata6a/pg_sjcszj/data ‘
#重启数据库
/usr/local/pgsql-12/bin/pg_ctl restart -D /pgdata6a/pg_sjcszj/data
#postgres.conf
listen_addresses = '*'
port = 15435
max_connections = 100
huge_pages = try
dynamic_shared_memory_type = posix
wal_level = replica
max_wal_size = 1GB
min_wal_size = 320MB
archive_mode = on
archive_command = '/ /pgdata6a/pg_sjcszj/archive.sh %f %p'
max_wal_senders = 10
log_destination = 'csvlog'
logging_collector = on
log_directory = 'log' # directory where log files are written,
log_filename = 'postgresql-%a.csv' # 'postgresql-%Y-%m-%d_%H%M%S.log'
log_truncate_on_rotation = on
log_rotation_age = 7d
log_rotation_size = 0
log_min_duration_statement =20000
log_line_prefix = '%m [%p] ' # special values:
log_timezone = 'PRC'
datestyle = 'iso, ymd'
timezone = 'PRC'
lc_messages = 'en_US.UTF8' # 'zh_CN.UTF-8'
lc_monetary = 'en_US.UTF-8' # locale for monetary formatting
lc_numeric = 'en_US.UTF-8' # locale for number formatting
lc_time = 'en_US.UTF-8' # locale for time formatting
default_text_search_config = 'pg_catalog.english'
#shared_preload_libraries = 'pg_stat_statements,pg_prometheus,timescaledb' # (change requires restart)
track_io_timing=on
pg_stat_statements.max=10000
pg_stat_statements.track = all
pg_stat_statements.save=on
二. 源码安装PostgreSQL9.5
主机环境:centos7.9.2009
主机地址:192.168.5.132
下载地址:https://ftp.postgresql.org/pub/source/v9.5.25/postgresql-9.5.25.tar.gz
yum -y install readline-devel zlib*
groupadd postgres && useradd -g postgres -m postgres
tar -zxf postgresql-9.5.25.tar.gz
cd postgresql
./configure -prefix=/usr/local/pgsql-9.5
make & make install
mkdir -p /pgdata6a/pg_sjcszj/data95 && chown -R postgres. /pgdata6a/pg_sjcszj/data95
su - postgres
/usr/pgsql-9.5/bin/initdb /var/lib/pgsql/9.5/data2
/usr/local/pgsql-9.5/bin/pg_ctl start -D /pgdata6a/pg_sjcszj/data95/
/usr/pgsql-9.5/bin/psql -h 192.168.5.130 -p 5432 -U rep -d postgres
三. PG9.5数据迁移及升级到PG12.5
主库生成同步用户
su - postgres
/usr/pgsql-9.5/bin/psql -p 5432
create role repl with login replication password '123456m';
添加白名单
vim /var/lib/pgsql/9.5/data/pg_hba.conf
host all all 0.0.0.0/0 md5
host replication repl 0.0.0.0/0 trust
vim postgresql.conf
max_connections = 100
max_wal_senders = 10 #(max_wal_senders < max_connections )
max_replication_slots = 5
wal_level = logical
fsync = on
/usr/pgsql-9.5/bin/pg_ctl restart -D /var/lib/pgsql/9.5/data
新建一台pg9.5实例 (源码安装)
- 在新主机上 备份数据库
/usr/local/pgsql-9.5/bin/pg_basebackup -h 192.168.5.130 -p 5432 -U repl -F p -P -X f -R -D /pgdata6a/pg_sjcszj/data2
/usr/local/pgsql-9.5/bin/pg_ctl start -D /pgdata6a/pg_sjcszj/data2
/usr/local/pgsql-9.5/bin/pg_controldata -D /pgdata6a/pg_sjcszj/data2
#Database cluster state: in archive recovery
主库上执行:
/usr/pgsql-9.5/bin/pg_controldata -D /var/lib/pgsql/9.5/data
#数据库簇状态: 在运行中
postgres=# select * from pg_stat_replication;
# select pg_wal_lsn_diff(pg_current_wal_lsn (),replay_lsn) from pg_stat_replication; #9.5上无效查询语句
备库执行:将状态改为生产
/usr/local/pgsql-9.5/bin/pg_ctl promote -D /pgdata6a/pg_sjcszj/data2
/usr/local/pgsql-9.5/bin/pg_controldata -D /pgdata6a/pg_sjcszj/data2
#复制配置文件
scp 192.168.5.130:/var/lib/pgsql/9.5/data/pg_hba.conf /pgdata6a/pg_sjcszj/
scp 192.168.5.130:/var/lib/pgsql/9.5/data/postgresql.conf /pgdata6a/pg_sjcszj/
#关闭备库
/usr/local/pgsql-9.5/bin/pg_ctl stop -D /pgdata6a/pg_sjcszj/data2
#升级
/usr/local/pgsql-12/bin/pg_upgrade -b /usr/local/pgsql-9.5/bin/ -B /usr/local/pgsql-12/bin/ -d /pgdata6a/pg_sjcszj/data2 -D /pgdata6a/pg_sjcszj/data -j 5 -r -k -c -p 5432 -P 15432
/usr/local/pgsql-12/bin/pg_ctl start -D /pgdata6a/pg_sjcszj/data
12 postgre.config 配置:
yum 安装 9.5
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql95-server
sudo /usr/pgsql-9.5/bin/postgresql95-setup initdb
sudo systemctl enable postgresql-9.5
sudo systemctl start postgresql-9.5
su - postgres
psql
ALTER USER postgres WITH PASSWORD 'postgres';
yum安装12
Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Install PostgreSQL:
sudo yum install -y postgresql12-server
Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl enable postgresql-12
修改端口:
vim /var/lib/pgsql/9.5/data/postgresql.conf
port = 15432
sudo systemctl start postgresql-12
su - postgres
psql --port=15432
ALTER USER postgres WITH PASSWORD 'postgres';
开通原创访问:
新增:
vim /var/lib/pgsql/9.5/data/pg_hba.conf
host all all 0.0.0.0/0 md5
修改:
vim /var/lib/pgsql/9.5/data/postgresql.conf
listen_addresses='*'