centos7 yum安装postgresql

[root@docker-remote1 ~]# yum install postgresql-server postgresql -y
[root@docker-remote1 ~]# service postgresql initdb # 初始化
[root@docker-remote1 ~]# service postgresql start
[root@docker-remote1 ~]# chkconfig postgresql on

# 默认不能用root进入pg的命令行,安装会创建一个用户 postgres
su 切换为 postgres 用户
执行 psql 可以进入pg的命令行

# 查看数据库
postgres=# \l     #通过\?可以看支持命令

开启远程登陆:

[root@docker-remote2 data]# cat /var/lib/pgsql/data/pg_hba.conf | grep -v "#" | grep -v "^$"
local all all peer
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
# 所有库,所有用户,从所有地址登陆
host all all 0.0.0.0/0 md5

修改监听地址等

[root@docker-remote2 data]# cat postgresql.conf | grep -v -E "#|^$"
# 监听所有地址
listen_addresses = '*' # 可以放开远程连接
log_timezone = 'PRC'
datestyle = 'iso, mdy'
timezone = 'PRC'
default_text_search_config = 'pg_catalog.english'

一些搜索来的操作命令:

# 切换到 postgres 用户
[root@docker-remote2 ~]# su postgres

# 创建用户aaa ,密码 aaa
postgres=# create user aaa with password ‘aaa’;
CREATE ROLE

# 创建数据库 venus
postgres=# create database venus;
CREATE DATABASE

# 把 venus 库赋给用户 aaa 权限
postgres=# grant all privileges on database venus to aaa;

# 把venus2库属主赋给aaa
postgres=# create database venus2 owner aaa;
CREATE DATABASE

postgresql-9.6 同步复制

参考:http://blog.csdn.net/baiyinqiqi/article/details/47951687

主配置:

同步模式基于异步模式添加一个参数即可。

异步配置文档:https://www.rootop.org/pages/3928.html

先说一个其中比较重要的参数synchronous_commit,同步流复制模式需要打开这个参数。
synchronous_commit = on #默认是注释的,此功能默认是开启的。从写完以后告诉主完成才算插入成功

这个参数对性能影响还是比较大的(毕竟等于写2台机器),根据实际情况可考虑关闭,在关键数据更新时在事务中将其暂时性打开,保证关键数据不会因意外停机而丢失。

使用同步流复制模式,那么master会等待同步slave返回事务状态后,才会完成当前事务。所以如果slave停掉,那么master的事务会一直等待下去。
追加一行参数:
[root@localhost ~]# vi /var/lib/pgsql/9.6/data/postgresql.conf
synchronous_standby_names = ‘standby1’ # 这个值用来控制同步的从机应用名,这个值从从机查

从配置:
[root@localhost ~]# cat /var/lib/pgsql/9.6/data/recovery.conf
standby_mode = ‘on’
primary_conninfo = ‘user=rep password=rep host=192.168.10.50 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres application_name=standby1

在 primary_conninfo 配置项段加了一段 application_name=standby1
standby1 需要追加到主服务器的synchronous_standby_names配置中。

重启主从。
在主上写数据测试->同步到从
停掉从机,再在主写数据就会卡主。直到从机恢复。

postgresql-9.6 异步复制

参考:http://blog.csdn.net/wzyzzu/article/details/53331206

官网:https://www.postgresql.org/download/linux/redhat/

环境:
master   192.168.10.50    centos7
slave       192.168.10.51    centos7

主:
安装,根据官网文档安装
[root@localhost ~]# yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm
[root@localhost ~]# yum install -y postgresql96-server postgresql96
[root@localhost ~]# /usr/pgsql-9.6/bin/postgresql96-setup initdb  #初始化数据库
[root@localhost ~]# systemctl enable postgresql-9.6
[root@localhost ~]# systemctl start postgresql-9.6
[root@localhost ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 557/sshd
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 2272/postmaster
tcp6 0 0 :::22 :::* LISTEN 557/sshd
tcp6 0 0 ::1:5432 :::* LISTEN 2272/postmaster

# 切换到 postgres 用户,创建复制账号。这里给了超级权限
[root@localhost ~]# su postgres
bash-4.2$ psql

# 用户名 rep 密码 rep
postgres=# create user rep superuser password ‘rep’;
CREATE ROLE

# 查看用户
postgres=# \du
List of roles
Role name | Attributes | Member of
———–+————————————————————+———–
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
rep | Superuser | {}

# 退出
postgres=# \q
bash-4.2$ exit

# 修改 postgresql.conf 配置文件
[root@localhost ~]# vi /var/lib/pgsql/9.6/data/postgresql.conf
# 监听地址,否则无法远程连接
listen_addresses = ‘*’

# 这个是设置主为wal的主机
wal_level = hot_standby

# 设置最多有几个流复制连接,有几个从就设置几个
max_wal_senders = 1

# 设置一个尽量大的值,以防止主库生成WAL日志太快,日志还没有来得及传送到standby就被覆盖 一个WAL日志文件是16M
wal_keep_segments = 64

# 设置流复制主机发送数据的超时时间
wal_sender_timeout = 120s

# 从库的max_connections必须要大于主库的
max_connections = 100

保存,退出。

修改 pg_hba.conf 配置文件,追加一行
[root@localhost ~]# vi /var/lib/pgsql/9.6/data/pg_hba.conf
host replication rep 192.168.10.51/32 md5
允许rep用户从ip 192.168.10.51 来获取wal日志

重启服务
[root@localhost ~]# systemctl restart postgresql-9.6

从库:
安装,注意没有初始化这步
[root@localhost ~]# yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm
[root@localhost ~]# yum install -y postgresql96-server postgresql96
[root@localhost ~]# systemctl enable postgresql-9.6

用pg_basebackup命令行工具在从库上生成基础备份,并生成 recovery.conf 配置文件。可以省去手动配置。

[root@localhost ~]# pg_basebackup -F p -R --progress -D /var/lib/pgsql/9.6/data -h 192.168.10.50 -p 5432 -U rep --password
Password: 输入密码
22826/22826 kB (100%), 1/1 tablespace
NOTICE: WAL archiving is not enabled; you must ensure that all required WAL segments are copied through other means to complete the backup

参数说明(可以通过pg_basebackup –help进行查看)
-h指定连接的数据库的主机名或IP地址
-U指定连接的用户名,此处是主库创建的rep用户
-F指定了输出的格式,支持p(原样输出)或者t(tar格式输出)
-x表示备份开始后,启动另一个流复制连接从主库接收WAL日志。
-P表示允许在备份过程中实时的打印备份的进度。
-R表示会在备份结束后自动生成recovery.conf文件,避免手动创建。
-D指定把备份写到哪个目录,注意做基础备份之前从库的数据目录 /var/lib/pgsql/9.6/data 下需要是空的,或者手动清空。

这样在 /var/lib/pgsql/9.6/data 目录下,可以省去配置 recovery.conf 步骤

编辑从库配置文件
[root@localhost ~]# vi /var/lib/pgsql/9.6/data/postgresql.conf

# 说明这台机器不仅用于数据归档,也用于数据查询
hot_standby = on

# 最大连接数,大于主
max_connections = 1000

# 数据流备份的最大延迟时间
max_standby_streaming_delay = 30s

# 多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
wal_receiver_status_interval = 10s

# 如果有错误的数据复制,是否向主进行反馈
hot_standby_feedback = off

保存,退出。

修改属主,否则启动服务会失败
[root@localhost ~]# chown -R postgres:postgres /var/lib/pgsql/9.6/data

启动服务
[root@localhost ~]# systemctl start postgresql-9.6

上查看进程,确保有 wal sender process 字样
[root@localhost pg_xlog]# ps aux | grep postgre
postgres 4725 0.0 0.8 357580 15280 ? Ss 12:49 0:00 /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data/
postgres 4727 0.0 0.0 210460 1560 ? Ss 12:49 0:00 postgres: logger process
postgres 4729 0.0 0.1 357580 3220 ? Ss 12:49 0:00 postgres: checkpointer process
postgres 4730 0.0 0.1 357580 2964 ? Ss 12:49 0:00 postgres: writer process
postgres 4731 0.0 0.3 357580 6128 ? Ss 12:49 0:00 postgres: wal writer process
postgres 4732 0.0 0.1 358004 2816 ? Ss 12:49 0:00 postgres: autovacuum launcher process
postgres 4733 0.0 0.1 212580 1916 ? Ss 12:49 0:00 postgres: stats collector process
postgres 4963 0.0 0.1 358376 3312 ? Ss 13:07 0:00 postgres: wal sender process rep 192.168.10.51(35022) streaming 0/3000220

上查看进程,有 wal receive 字样
[root@localhost ~]# ps aux | grep postgre
postgres 3195 0.0 1.4 375892 27360 ? Ss 13:07 0:00 /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data/
postgres 3197 0.0 0.0 210460 1552 ? Ss 13:07 0:00 postgres: logger process
postgres 3198 0.0 0.1 375956 2400 ? Ss 13:07 0:00 postgres: startup process recovering 000000010000000000000003
postgres 3199 0.2 0.1 383044 3600 ? Ss 13:07 0:00 postgres: wal receiver process streaming 0/3000220
postgres 3200 0.0 0.1 375892 1916 ? Ss 13:07 0:00 postgres: checkpointer process
postgres 3201 0.0 0.1 375892 1924 ? Ss 13:07 0:00 postgres: writer process

这样主从复制就可以了。

登陆两台pg数据库,创建表,插入数据,都会同步到从库。

注意:

pg需要编辑 /var/lib/pgsql/9.6/data/pg_hba.conf 加一条 host all rep 0.0.0.0/0 md5 才能用rep远程登陆pg数据库。否则用户是无法远程登陆的。

在centos下编译PostgreSQL数据库

PostgreSQL官网:http://www.postgresql.org

readline是一个开源的跨平台程序库,提供了交互式的文本编辑功能。postgresql需要readline的支持。
wget -c https://ftp.postgresql.org/pub/source/v9.3.5/postgresql-9.3.5.tar.gz
[root@rootop postgresql-9.3.5]# yum install readline readline-devel

[root@rootop postgresql-9.3.5]# ./configure --prefix=/usr/local/pgsql

[root@rootop postgresql-9.3.5]# make
[root@rootop postgresql-9.3.5]# make install

添加系统账户:
[root@rootop ~]# useradd postgres
[root@rootop ~]# passwd postgres

创建数据目录:
[root@rootop ~]# mkdir /usr/local/pgsql/data
[root@rootop ~]# chown postgres:postgres /usr/local/pgsql/data/

初始化数据库:
[root@rootop ~]# su postgres  #切换到postgres用户执行
[postgres@rootop ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
根据提示可以通过 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data/
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start 启动服务。
推荐下面的脚本启动方式,启动以后会在tcp上监听5432端口。
[postgres@rootop ~]$ lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 5140 postgres 3u IPv4 2394876345 0t0 TCP localhost:postgres (LISTEN)

复制管理脚本(root操作):
[root@rootop postgresql-9.3.5]# cp contrib/start-scripts/linux /etc/init.d/postgresql
[root@rootop postgresql-9.3.5]# chmod o+x /etc/init.d/postgresql
编辑启动脚本,注意以下部分为实际信息:
#安装路径
prefix=/usr/local/pgsql
#数据目录
PGDATA=”/usr/local/pgsql/data”
#启动用户
PGUSER=postgres
#日志路径
PGLOG=”$PGDATA/serverlog”
然后就可以通过service postgresql start|stop|restart|reload|status 管理了。

开机启动:
[root@AY131126202614070132Z ~]# chkconfig postgresql on

相关配置文件:
通过 /usr/local/pgsql/data/postgresql.conf 可以配置监听地址、端口及连接数等。
listen_addresses =
port =
max_connections =
通过 /usr/local/pgsql/data/pg_hba.conf 可以配置允许远程连接的地址。
host all all 127.0.0.1/32 trust

登陆数据库:

[root@AY131126202614070132Z ~]# /usr/local/pgsql/bin/psql -h 127.0.0.1 -d postgres -U postgres
psql (9.3.5)
Type "help" for help.

postgres=# \l  #查看已有的数据库
 List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
 | | | | | postgres=CTc/postgres
 template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
 | | | | | postgres=CTc/postgres
(3 rows)

postgres=# \q   #退出
psql 支持的参数可以通过/usr/local/pgsql/bin/psql --help 获取

安装完成。

附手册:http://www.php100.com/manual/PostgreSQL8/tutorial.html