安装postgresql
在线安装基础环境:
yum -y install gcc
yum -y install gcc-c++
yum -y install readline-devel
yum -y install zlib-devel
yum -y install make
解压源码到/app/db/
cd /app/db
tar -zxvf postgresql-10.3.tar.gz
创建用户及密码:
adduser postgres
passwd postgres
安装:
cd /app/db/postgresql-10.3
./configure --prefix=/app/db/postgresql
如果途中找不到readline-devel依赖 执行下一条命令
./configure --prefix=/app/db/postgresql --without-readline
make
make install
修改环境变量
vi /etc/profile
PATH=$PATH:/app/db/postgresql/bin
初始化数据库:
mkdir data
chown postgres:postgres /app/db/postgresql/data/
su postgres
/app/db/postgresql/bin/initdb -D /app/db/postgresql/data/
复制并修改配置文件:
cp /app/db/postgresql/postgresql-10.3/contrib/start-scripts/linux /etc/init.d/postgresql
vi /etc/init.d/postgresql
修改内容为:
prefix=/app/db/postgresql
PGDATA="/app/db/postgresql/data"
chmod +x /etc/init.d/postgresql
启动数据库和设置开机自启动:
/etc/init.d/postgresql start
chkconfig postgresql on
创建操作数据库的历史文件 并授权
touch /app/db/postgresql/.pgsql_history
chown postgres:postgres /app/db/postgresql/.pgsql_history
修改数据库外网访问:
/etc/init.d/postgresql stop
修改pg_hba.conf
find / -name pg_hba.conf
vi /app/db/postgresql/data/pg_hba.conf
将默认的
host all all 127.0.0.1/32 trust
修改为
host all all 192.168.1.1/32 trust #IP全匹配
或
host all all 192.168.1.1/24 trust #IP匹配前三段
或
host all all 0.0.0.0/0 trust #全部允许
修改postgresql.conf
listen_addresses = 'localhost'
为
listen_addresses = '*'
查看端口:
losf -i:5432
搭建PostgreSQL 主备环境:
10.11.13.15主
10.11.13.16备
主环境配置:
启动服务
/etc/init.d/postgresql start
创建用于主备之间通信用的用户
CREATE ROLE replication login replication encrypted password 'replication';
修改pg_hba.conf 文件,添加如下内容,允许两台计算机上的复制用户和超级用户登录:
host replication replication 10.11.13.15/32 md5
host replication replication 10.11.13.16/32 md5
host all postgres 10.11.13.15/32 trust
host all postgres 10.11.13.16/32 trust
在主节点的 postgresql.conf 中设置这些参数:
max_wal_senders =10
wal_level = replica
wal_log_hints = on
log_connections = on
wal_receiver_status_interval = 2s
hot_standby_feedback = on
配置之后重新启动服务:
/etc/init.d/postgresql stop
/etc/init.d/postgresql start
备环境配置:
停止服务
/etc/init.d/postgresql stop
首先删除备节点中的数据目录 /app/db/postgresql/data中的文件:
cd /app/db/postgresql/data
rm –rf *
然后执行:
pg_basebackup -d "hostaddr=10.11.13.15 port=5432 user=nantian password=nantian" -D /app/db/postgresql/data -v -Fp -Xs
基础备份完成后,修改备节点的 postgresql.conf 文件
设置:
hot_standby = on
将 /app/db/postgresql-10.3/share/ 中的 recovery.conf.sample 拷贝到 /app/db/postgresql/data 下,
重命名为 recovery.conf:
cp /app/db/postgresql-10.3/share/recovery.conf.sample /app/db/postgresql/data/recovery.conf
并修改以下配置:
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=10.11.13.15 port=5432 user=nantian password=nantian'
trigger_file = 'tgfile’
重启服务:
/etc/init.d/postgresql start
开启防火墙:
systemctl start firewalld
开放端口 并重新加载防火墙:
firewall-cmd --zone=public --add-port=5432/tcp --permanent
firewall-cmd --reload
备注
备份数据库:
pg_dump postgres > /usr/local/postgresql/data/20180720_db.sql
导入数据库:
psql -d postgres -f /usr/local/postgresql/data/20180720-db.sql