Postgresql
常用操作命令:
需要超级用户登录到数据库中
库的基本操作
\l #显示库
\c dbname #进入库
\d #查看表
创建只读账号:
create user ro_user password 'readonly';
# 设置Postgres数据库为只读的transaction
alter user ro_user set default_transaction_read_only=on;
# 赋予用户权限,查看public模式下所有表:(新建表也会有只读权限)
grant usage on schema public to ro_user;
alter default privileges in schema public grant select on tables to ro_user;
授权只读账号slave:
# 赋予用户连接数据库权限 (首次需要配置)
grant connect on database zhong to ro_user;
# 切换到指定数据库
\c zhong
#给pg中salve只读用户添加权限
grant USAGE on SCHEMA public to slave; #public是Schema ,可以通过\c dbname ; \d来查看表在的Schema
grant SELECT on ALL tables in schema public to slave
#序列查看权限授权
grant SELECT on ALL sequences in schema public to slave
PostgreSQL是一个比较高性能的数据库, 结合PostGIS插件, 使PostgreSQL成为了一个空间数据库,能够进行空间数据管理、数量测量与几何拓扑分析。PostgreSQL从9.3开始支持JSON数据类型, 9.4开始支持JSONB, 具备NoSQL数据库功能, 在性能上甚至超过MongoDB。
二进制包下载地址
https://www.postgresql.org/ftp/source/
此处下载版本13.1
https://ftp.postgresql.org/pub/source/v13.1/postgresql-13.1.tar.gz
1、安装-所有节点安装
腾讯云服务器,系统版本centos7.9 2c4g
1.1 部署规划
- 主节点: 172.21.48.6
- 从节点: 172.21.48.5
1.2 安装
在主从部署安装之前, 先要在两台节点上分别都安装Postgresql, 这里以安装Postgresql13.1版本为例。
yum install gcc-c++ -y
yum install perl-ExtUtils-Embed -y
yum install readline-devel -y
yum install zlib-devel -y
yum install python-devel -y
创建用户
groupadd postgres
useradd -g postgres postgres
echo "postgres"|passwd postgres --stdin
解压,编译安装
root@server01 ~]# tar xf postgresql-13.1.tar.gz
[root@server01 ~]# cd postgresql-13.1/
./configure --prefix=/usr/local/pgsql13.1 --with-perl --with-python
make && make install
ln -sf /usr/local/pgsql13.1 /usr/local/pgsql
chown -R postgres:postgres /usr/local/pgsql13.1/
chown -R postgres:postgres /usr/local/pgsql/
安装contrib目录下的工具
cd postgresql-13.1/contrib
make && make install
切换用户postgres,创建环境变量
su - postgres
vim .bash_profile
export PATH=/usr/local/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib
export PGHOST=/tmp
export LANG=en_US.UTF-8
export PGDATA=/postgres/
vim /etc/profile
export PATH=/usr/local/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH
1.3 数据库实例化
[root@server01 contrib]# mkdir -p /data/appData/postgreSql
[root@server01 contrib]# chown -R postgres:postgres /data/appData/postgreSql
su - posrgres
export PGDATA=/data/appData/postgreSql
initdb -k #对于数据可靠性要求很高的尝尽,建议打开数据块checksum校验功能
[postgres@server02 ~]$ /usr/local/pgsql13.1/bin/initdb -D /data/appData/postgreSql --encoding=UTF8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /data/appData/postgreSql ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
/usr/local/pgsql13.1/bin/pg_ctl -D /data/appData/postgreSql -l logfile start
1.4 启动数据库
###1.启动服务: 添加-l logfile start 可以查看日志
#这种情况下服务是监听在127.0.0.1:5432下,所以需要修改配置文件,配置远程可以连接
[postgres@server01 ~]$ pg_ctl start -D /data/appData/postgreSql/
waiting for server to start....2023-06-24 22:00:22.629 CST [20378] LOG: starting PostgreSQL 13.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2023-06-24 22:00:22.629 CST [20378] LOG: listening on IPv6 address "::1", port 5432
2023-06-24 22:00:22.629 CST [20378] LOG: listening on IPv4 address "127.0.0.1", port 5432
2023-06-24 22:00:22.632 CST [20378] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2023-06-24 22:00:22.634 CST [20379] LOG: database system was shut down at 2023-06-24 21:52:19 CST
2023-06-24 22:00:22.637 CST [20378] LOG: database system is ready to accept connections
done
server started
#####查看端口监听的情况
[postgres@server01 ~]$ netstat -ntlp
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
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 -
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 20378/postgres
tcp6 0 0 ::1:5432 :::* LISTEN 20378/postgres
###2.然后停止服务
[postgres@server01 ~]$ pg_ctl stop -D /data/appData/postgreSql/
waiting for server to shut down....2023-06-24 22:03:24.367 CST [20378] LOG: received fast shutdown request
2023-06-24 22:03:24.368 CST [20378] LOG: aborting any active transactions
2023-06-24 22:03:24.369 CST [20378] LOG: background worker "logical replication launcher" (PID 20385) exited with exit code 1
2023-06-24 22:03:24.369 CST [20380] LOG: shutting down
2023-06-24 22:03:24.378 CST [20378] LOG: database system is shut down
done
server stopped
##
####修改远程可以连接
修改listen监听为0.0.0.0
[postgres@server01 ~]$ cat /data/appData/postgreSql/postgresql.conf | grep listen
listen_addresses = '0.0.0.0' # what IP address(es) to listen on;
####修改pg_hba.conf,增加host all all 0.0.0.0/0 trust
[postgres@server01 ~]$ cat /data/appData/postgreSql/pg_hba.conf | grep host | grep -v "^#"
host all all 0.0.0.0/0 trust
host all all ::1/128 trust
host all all 0.0.0.0/0 trust
[postgres@server01 ~]$
创建测试账号-此部可以忽略
# 创建用户
createuser -U commonuser -h127.0.0.1 -p5432 -s postgres
psql -d postgres -U commonuser -h127.0.0.1 -p5432 -c "create user synthetic_user with superuser password '123456';"
2、主从同步-主库操作
主库操作
一定要保存好这个文件 all_DB.sql
2.2 创建主 从同步用户
create role repl login replication encrypted password '123456'; ###非常关键
如果已安装postgresql建议先进行备份操作,备份操作参考如下:
# 创建备份SQL文件存放目录
mkdir -p /data/appData/backup /data/app/backup /data/tmp
## 导出系统库-非常关键
cd /data/appData/postgreSql/
pg_dumpall -U postgres -h 127.0.0.1 -p 5432 -f /data/appData/postgreSql/all_DB.sql
# 停止服务
pg_ctl stop -D /data/appData/postgreSql/
# 备份文件
cd /data/app
cp -arp /data/app/postgreSql /data/app/backup/
cp -arp /data/appData/postgreSql /data/appData/backup/
# 创建主从同步用户
create role repl login replication encrypted password '123456';
2.3 pg_hba.conf 配置
# 添加从库网段
echo "host replication repl 172.21.48.5/24 md5" >> /data/appData/postgreSql/pg_hba.conf
# 查看
[postgres@server01 ~]$ cat /data/appData/postgreSql/pg_hba.conf | grep md5
# METHOD can be "trust", "reject", "md5", "password", "scram-sha-256",
# Note that "password" sends passwords in clear text; "md5" or
host replication repl 172.21.48.5/24 md5
2.4 postgresql.conf配置
参考配置
mkdir -p /data/appData/postgreSql/pg_archive/xlog_files
vim /data/appData/postgreSql/postgresql.conf
listen_addresses = '*' # 监听所有ip
wal_level = hot_standby # 热备模式
archive_mode = on #允许归档
archive_command = 'cp %p /data/appData/postgreSql/pg_archive/xlog_files/%f' # 此目录需要创建
max_wal_senders = 32 #同步最大的进程数量
wal_sender_timeout = 60s #流复制主机发送数据的超时时间
max_connections = 200 #最大连接数,从库的max_connections必须要大于主库的
2.5 重启主库
pg_ctl restart -D /data/appData/postgreSql/