节点名称 | 操作系统 | ip | 内存 | cpu核数 | postgresql版本 |
---|---|---|---|---|---|
pg-master | Centos7.6 | 192.168.233.30 | 8G | 4核 | postgresql-11.13.tar.gz |
pg-standby | Centos7.6 | 192.168.233.31 | 8G | 4核 | postgresql-11.13.tar.gz |
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
systemctl mask firewalld #屏蔽FirewallD服务
systemctl stop firewalld #停用FirewallD服务
yum -y install iptables-services #安装
vi /etc/sysconfig/iptables #编辑防火墙配置文件
#增加这个放通5432端口
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5432 -j ACCEPT
systemctl restart iptables.service #最后重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
/usr/libexec/iptables/iptables.init restart #重启防火墙
vi /etc/selinux/config
SELINUX=disabled # 这里置为disabled
# SELINUXTYPE=targeted # 这里备注掉
setenforce 0 #使配置立即生效
hostname pg-master
hostnamectl set-hostname pg-master
vi /etc/hostname #编辑配置文件
pg-master
vi /etc/hosts #编辑配置文件
127.0.0.1 localhost pg-master
192.168.233.30 pg-master
192.168.233.31 pg-standby
hostname pg-standby
hostnamectl set-hostname pg-standby
vi /etc/hostname #编辑配置文件
pg-standby
vi /etc/hosts #编辑配置文件
127.0.0.1 localhost pg-standby
192.168.233.30 pg-master
192.168.233.31 pg-standby
PostgreSQL版本:postgresql-11.13.tar.gz
yum -y install tcl tcl-devel uuid-devel perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake gcc* readline-devel
mkdir -p /usr/local/pgsql #创建安装目录
mkdir -p /usr/local/pgsql/data #创建数据库存放目录
cd /usr/local/src
rz #上传源码包
tar zxvf postgresql-11.13.tar.gz #解压
cd postgresql-11.13
./configure --prefix=/usr/local/pgsql --with-openssl --with-pgport=5432 --with-tcl --with-perl --with-python --with-libxml --with-libxslt --with-ossp-uuid --with-pam --with-ldap
gmake world #gmake包括第三方插件全部编译
gmake install-world #包括第三方插件全部安装
#创建PostgreSQL运行用户,PostgreSQL不允许使用root用户运行服务
#创建用户群组postgres
groupadd postgres
useradd -g postgres postgres #创建用户postgres,并加入postgres组
chown postgres.postgres -R /usr/local/pgsql
chown postgres.postgres -R /usr/local/pgsql/data
vi /etc/profile
#添加如下4行内容
export PGHOME=/usr/local/pgsql
export PGDATA=/usr/local/pgsql/data
export PATH=$PATH:$PGHOME/bin
export LD_LIBRARY_PATH=$PGHOME/lib
source /etc/profile #使生效
su - postgres #切换到postgres用户
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data --encoding=UTF8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 #初始化数据库
vi /usr/local/pgsql/data/postgresql.conf
port = 5432 #端口号
max_connections = 1000 #最大连接数
listen_addresses = '*' #监听本机所有ip,也可以按需设置
wal_log_hints = on
full_page_writes = on
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start #启动
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile stop #停止
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile restart #重启
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile status #查看状态
exit #切换到root
cp /usr/local/src/postgresql-11.13/contrib/start-scripts/linux /etc/init.d/postgresql #拷贝启动文件
chmod +x /etc/init.d/postgresql #设置运行权限
vi /etc/init.d/postgresql #编辑修改
prefix=/usr/local/pgsql #安装目录
PGDATA="/usr/local/pgsql/data" #数据库存放目录
PGUSER=postgres #运行用户
service postgresql start
service postgresql restart
service postgresql stop
chkconfig postgresql on
vi /usr/lib/systemd/system/postgresql.service #使用Systemd启动
[Unit]
Description=The PostgreSQL Database Server
After=syslog.target
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
ExecStart=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start
ExecStop=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data stop
ExecRestart=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data restart
ExecReload=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data reload
ExecStatus=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data status
TimeoutSec=300
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start postgresql
systemctl enable postgresql
systemctl restart postgresql
systemctl status postgresql
su - postgres
psql
alter user postgres with password 'postgres';
\q
vi /usr/local/pgsql/data/pg_hba.conf #添加修改
#添加
host all all 127.0.0.1/32 trust #允许所有用户从本机免密访问所有数据库
host all postgres 0.0.0.0/0 md5 #允许postgres从任何ip以密码访问所有数据库
systemctl restart postgresql #重启服务
#进入控制台,创建数据库同步用户repl,密码为Aa123456
psql
create role repl with login replication encrypted password 'Aa123456';
\q
#设置数据库主从同步用户权限信息
vi /usr/local/pgsql/data/pg_hba.conf
#添加
host all repl 192.168.233.0/24 trust
host replication repl 192.168.233.0/24 md5
vi /usr/local/pgsql/data/postgresql.conf #修改数据库配置文件信息
wal_level = replica
archive_mode = on
archive_command = 'cp %p /usr/local/pgsql/data/pg_archive/%f'
wal_keep_segments = 10240
wal_sender_timeout = 60s
archive_command = 'gzip < %p > /usr/local/pgsql/data/pg_archive/%f.gz'
mkdir -p /usr/local/pgsql/data/pg_archive
systemctl restart postgresql #最后重启数据库
groupadd postgres #创建用户群组postgres
useradd -g postgres postgres #创建用户postgres,并加入postgres组
chown postgres.postgres -R /usr/local/pgsql
chown postgres.postgres -R /usr/local/pgsql/data
chmod 0700 /usr/local/pgsql/data
vi /etc/profile
#添加
export PGHOME=/usr/local/pgsql
export PGDATA=/usr/local/pgsql/data
export PATH=$PATH:$PGHOME/bin
export LD_LIBRARY_PATH=$PGHOME/lib
source /etc/profile #使生效
su - postgres #切换到postgres用户
/usr/local/pgsql/bin/pg_basebackup -Fp --progress -D /usr/local/pgsql/data -R -h 192.168.233.30 -p 5432 -U repl --password
#从模板文件拷贝到data目录
cp /usr/local/pgsql/share/recovery.conf.sample /usr/local/pgsql/data/recovery.conf
vi /usr/local/pgsql/data/recovery.conf
#去掉备注并修改
standby_mode = on #on为从库
primary_conninfo = 'host=192.168.233.30 port=5432 user=repl password=Aa123456' #对应主库信息
recovery_target_timeline = 'latest' #流复制同步最新数据
vi /usr/local/pgsql/data/postgresql.conf #修改从库postgresql.conf文件
max_connections = 1000 #最大连接数
hot_standby = on #说明这台机器不仅仅是用于数据归档,也用于数据查询
listen_addresses = '*' #监听本机所有ip,也可以按需设置
max_standby_streaming_delay = 30s #数据流备份的最大延迟时间
wal_receiver_status_interval = 10s #间隔时间
hot_standby_feedback = on #如果有错误的数据复制,是否向主进行反馈
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start #启动
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile stop #停止
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile restart #重启
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile status #查看状态
exit #切回root
cp /usr/local/src/postgresql-11.13/contrib/start-scripts/linux /etc/init.d/postgresql #拷贝启动文件
chmod +x /etc/init.d/postgresql #设置运行权限
vi /etc/init.d/postgresql #编辑修改
prefix=/usr/local/pgsql #安装目录
PGDATA="/usr/local/pgsql/data" #数据库存放目录
PGUSER=postgres #运行用户
service postgresql start
service postgresql restart
service postgresql stop
chkconfig postgresql on
vi /usr/lib/systemd/system/postgresql.service #使用Systemd启动
[Unit]
Description=The PostgreSQL Database Server
After=syslog.target
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
ExecStart=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start
ExecStop=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data stop
ExecRestart=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data restart
ExecReload=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data reload
ExecStatus=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data status
TimeoutSec=300
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start postgresql
systemctl enable postgresql
systemctl restart postgresql
systemctl status postgresql
su - postgres
psql
select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
----------------+------------
192.168.233.31 | async
(1 row)
\x on
Expanded display is on.
select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid | 45833
usesysid | 16384
usename | repl
application_name | walreceiver
client_addr | 192.168.233.31
client_hostname |
client_port | 49816
backend_start | 2022-07-13 08:07:25.080263+00
backend_xmin | 570
state | streaming
sent_lsn | 0/3000140
write_lsn | 0/3000140
flush_lsn | 0/3000140
replay_lsn | 0/3000140
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async
\q
psql
create database pgtest;
\connect pgtest
\q
su - postgres
psql
\connect pgtest
\q
/usr/local/pgsql/bin/pg_controldata /usr/local/pgsql/data/| grep 'Database cluster state'
Database cluster state: in production
/usr/local/pgsql/bin/pg_controldata /usr/local/pgsql/data/| grep 'Database cluster state'
Database cluster state: in archive recovery
su - postgres
systemctl stop postgresql #停止主库
/usr/local/pgsql/bin/pg_ctl stop -m fast #停止主库
ps -ef|grep postgres #查看数据库进程,已经看不到了
su - postgres
ps -ef|grep postgres #查看进程,数据库正常
/usr/local/pgsql/bin/pg_ctl status #查看数据库状态正常
/usr/local/pgsql/bin/postgres "-D" "/usr/local/pgsql/data"
/usr/local/pgsql/bin/pg_ctl promote #提升从库为新主库
ps -ef|grep postgres #查看进程,数据库正常
cd /usr/local/pgsql/data/
ls recovery.done
提升从库为主库之后,可以看到后台进程中没有startup recovering进程了,多了postgres: walwriter 写进程
之前的/usr/local/pgsql/data/recovery.conf文件自动更改为/usr/local/pgsql/data/recovery.done 这是告诉postgresql,我现在不再是从库了,我的身份是主库
psql
create table test as select 1 id;
\q
#允许新从库(原主库192.168.1.100)可以通过replica用户访问数据库(按理说已经有了,不用做,没有就添加)
vi /usr/local/pgsql/data/pg_hba.conf
## 添加
host replication repl 192.168.233.0/24 md5
host all repl 192.168.233.0/24 trust
cp /usr/local/pgsql/share/recovery.conf.sample /usr/local/pgsql/data/recovery.conf
vi /usr/local/pgsql/data/recovery.conf
#去掉备注并修改
recovery_target_timeline = 'latest' #流复制同步最新数据
standby_mode = on #on为从库
primary_conninfo = 'host=192.168.233.31 port=5432 user=repl password=Aa123456' #对应主库信息
chmod 0700 /usr/local/pgsql/data #设置权限,重要,否则无法启动
systemctl start postgresql #启动新从库(原主库)数据库
su - postgres
#将旧的数据库目录重命名
mkdir /usr/local/pgsql.old
chown -R postgres.postgres /usr/local/pgsql.old
mv /usr/local/pgsql/* /usr/local/pgsql.old/
exit #切回root
cd /opt
rz #上传源码包
tar -zxvf postgresql-11.16.tar.gz #解压
cd postgresql-11.16/ #进入到源码目录
./configure --prefix=/usr/local/pgsql --with-openssl --with-pgport=5432 --with-tcl --with-perl --with-python --with-libxml --with-libxslt --with-ossp-uuid --with-pam --with-ldap
gmake world #gmake包括第三方插件全部编译
gmake install-world #包括第三方插件全部安装
mkdir /usr/local/pgsql/data #创建数据目录
chown -R postgres.postgres /usr/local/pgsql
chown -R postgres.postgres /usr/local/pgsql/data #授权数据目录
su - postgres
#初始化数据库
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data --encoding=UTF8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 #初始化数据库
#如果有外部extension插件则在这安装插件
#关闭旧数据库
/usr/local/pgsql.old/bin/pg_ctl -D /usr/local/pgsql.old/data/ stop -m fast
#环境变量临时赋值本地ip
export PGHOST=127.0.0.1
#升级前检查
/usr/local/pgsql/bin/pg_upgrade -d /usr/local/pgsql.old/data/ -D /usr/local/pgsql/data/ -b /usr/local/pgsql.old/bin/ -B /usr/local/pgsql/bin/ -c
#升级
/usr/local/pgsql/bin/pg_upgrade -d /usr/local/pgsql.old/data/ -D /usr/local/pgsql/data/ -b /usr/local/pgsql.old/bin/ -B /usr/local/pgsql/bin/
#收集表的统计信息
./analyze_new_cluster.sh
cd $PGDATA
rm -f pg_hba.conf #删除新的pg_hba.conf
rm -f postgresql.conf #删除新的postgresql.conf
#拷贝旧的pg_hba.conf和postgresql.conf到升级的库
cp /usr/local/pgsql.old/data/pg_hba.conf $PGDATA/
cp /usr/local/pgsql.old/data/postgresql.conf $PGDATA/
#启动数据库
pg_ctl -D $PGDATA start
#旧版本
/database/postgres11.old/psql/bin/pg_config
BINDIR = /database/postgres11.old/psql/bin
DOCDIR = /database/postgres11.old/psql/share/doc
HTMLDIR = /database/postgres11.old/psql/share/doc
INCLUDEDIR = /database/postgres11.old/psql/include
PKGINCLUDEDIR = /database/postgres11.old/psql/include
INCLUDEDIR-SERVER = /database/postgres11.old/psql/include/server
LIBDIR = /database/postgres11.old/psql/lib
PKGLIBDIR = /database/postgres11.old/psql/lib
LOCALEDIR = /database/postgres11.old/psql/share/locale
MANDIR = /database/postgres11.old/psql/share/man
SHAREDIR = /database/postgres11.old/psql/share
SYSCONFDIR = /database/postgres11.old/psql/etc
PGXS = /database/postgres11.old/psql/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix=/database/postgres11/psql/' '--with-perl' '--with-python'
CC = gcc
CPPFLAGS = -D_GNU_SOURCE
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2
CFLAGS_SL = -fPIC
LDFLAGS = -Wl,--as-needed -Wl,-rpath,'/database/postgres11/psql/lib',--enable-new-dtags
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgcommon -lpgport -lpthread -lz -lreadline -lrt -lcrypt -ldl -lm
VERSION = PostgreSQL 11.6
#新版本
/database/postgres11/psql/bin/pg_config
BINDIR = /database/postgres11/psql/bin
DOCDIR = /database/postgres11/psql/share/doc/postgresql
HTMLDIR = /database/postgres11/psql/share/doc/postgresql
INCLUDEDIR = /database/postgres11/psql/include
PKGINCLUDEDIR = /database/postgres11/psql/include/postgresql
INCLUDEDIR-SERVER = /database/postgres11/psql/include/postgresql/server
LIBDIR = /database/postgres11/psql/lib
PKGLIBDIR = /database/postgres11/psql/lib/postgresql
LOCALEDIR = /database/postgres11/psql/share/locale
MANDIR = /database/postgres11/psql/share/man
SHAREDIR = /database/postgres11/psql/share/postgresql
SYSCONFDIR = /database/postgres11/psql/etc/postgresql
PGXS = /database/postgres11/psql/lib/postgresql/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix='
CC = gcc
CPPFLAGS = -D_GNU_SOURCE
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2
CFLAGS_SL = -fPIC
LDFLAGS = -Wl,--as-needed -Wl,-rpath,'/lib',--enable-new-dtags
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgcommon -lpgport -lpthread -lz -lreadline -lrt -lcrypt -ldl -lm
VERSION = PostgreSQL 11.16
详见:逻辑备份
详见:pg单机和主从部署
详见:数据库管理
详见:逻辑恢复
[postgres@pg_master ~]$ psql -h 192.168.233.30 -p 5432 -U postgres -d pgtest -E
# -h 主机名 默认读取环境变量 PGHOST PGHOST默认为当前主机
# -p 端口号 默认读取环境变量 PGPORT PGPORT默认为5432
# -U 用户名 默认读取环境变量 PGUSER PGUSER默认为postgres
# -d 数据库名 默认读取环境变量 PGDATABASE PGDATABASE默认为postgres
# -W 强制输入密码 当有配值环境变量 PGPASSWORD 时,无需输入密码,加入该参数后,强制用户登录时输入密码
# -E 回显命令对应的sql语句
命令 | 扩展 | 说明 |
---|---|---|
\l | \l+ | 查看数据库(详细信息) |
\d | \d+ | 查看所有对象 (详细信息) |
\dn | \dn+ | 查看所有模式(详细信息) |
\db | \dt+ | 查看所有表(详细信息) |
\df | \df+ | 查看所有函数(详细信息) |
\dv | \dv+ | 查看所有视图(详细信息) |
\du | \du+ | 查看所有用户及权限信息(详细信息) |
\set | 设置会话级别的参数;设置绑定变量,通过:调用 | |
\! | 执行外部命令 | |
\i | 执行sql脚本 | |
\connect | 以指定用户登入到指定数据库 | |
\q | 退出psql |
CREATE DATABASE name [ [WITH] [OWNER [=] user_name]
[TEMPLATE [=] template]
[ENCODING [=] encoding]
[LC_COLLATE [=] lc_collate]
[LC_CTYPE [=] lc_ctype]
[TABLESPACE [=] tablespace]
[CONNECTION LIMIT [=] connlimit ] ]
create database etl; --创建一个etl数据库,其他参数不用配置,直接用模板数据库的即可
drop database etl; --删除数据库
PostgreSQL 11.2 手册:CREATE ROLE
--创建管理员组 admin
create role admin;
--创建开发人员用户组 developer
create role developer;
--创建数据装载用户组 dataload
create role dataload;
--创建外部接口用户组 interface
create role interface;
--创建管理员 pgadmin
create role pgadmin with superuser login password 'pgadminAa123456';
--创建开发用户
create role yuzhenchao with login password 'yzc+Aa123456' connection limit 10 valid until '2023-01-16 00:00:00';
--创建数据装载用户
create role copyload with login password 'copy+Aa123456' connection limit 60 valid until '2023-01-16 00:00:00';
--创建外部接口用户
create role finebi with login password 'finebi+Aa123456' connection limit 20 valid until '2023-01-16 00:00:00';
--将pgadmin加入到admin组
alter group admin add user pgadmin;
--将yuzhenchao加入到developer组
alter group developer add user yuzhenchao;
--将copyload加入到dataload组
alter group dataload add user copyload;
--将finebi加入到interface用户组
alter group interface add user finebi;
--创建pgadmin对应的模式名pgadmin
create schema pgadmin;
--创建yuzhenchao对应的模式名yuzhenchao
create schema yuzhenchao;
--创建copyload对应的模式名copyload
create schema copyload;
--一般外部接口都只有只读权限,所以不需要给他建单独的模式
PostgreSQL 11.2 手册:GRANT
--将pgadmin模式的所有权限授权给pgadmin
grant create,usage on schema pgadmin to pgadmin;
--将yuzhenchao模式的所有权限授权给yuzhenchao
grant create,usage on schema yuzhenchao to yuzhenchao;
--将copyload模式的所有权限授权给copyload
grant create,usage on schema copyload to copyload;
--将pgadmin模式的usage权限授权给public
grant usage on schema pgadmin to public;
--将yuzhenchao模式的usage权限授权给public
grant usage on schema yuzhenchao to public;
--将copyload模式的usage权限授权给public
grant usage on schema copyload to public;
--任何用户都拥有public模式的所有权限
--出于安全,回收任何用户在public的create权限
revoke create on schema public from public;
/*
* pg中函数默认公开execute权限
* 通过pg的基于schema和基于role的默认权限实现
*/
--在schema为pgadmin上创建的任何函数,除定义者外,其他人调用需要显式授权
alter default privileges for role pgadmin revoke execute on functions from public;
--由pgadmin用户创建的任何函数,除定义者外,其他人调用需要显式授权
alter default privileges in schema pgadmin revoke execute on functions from public;
--在schema为yuzhenchao上创建的任何函数,除定义者外,其他人调用需要显式授权
alter default privileges for role yuzhenchao revoke execute on functions from public;
--由yuzhenchao用户创建的任何函数,除定义者外,其他人调用需要显式授权
alter default privileges in schema yuzhenchao revoke execute on functions from public;
--在schema为copyload上创建的任何函数,除定义者外,其他人调用需要显式授权
alter default privileges for role copyload revoke execute on functions from public;
--由copyload用户创建的任何函数,除定义者外,其他人调用需要显式授权
alter default privileges in schema copyload revoke execute on functions from public;
/*
* pg与oracle不同,没有select any table的权限
* 但是pg有默认权限
* 通过pg的基于schema和基于role的默认权限实现
*/
--在schema为pgadmin上创建的任何表默认公开select权限
alter default privileges in schema pgadmin grant select on tables to public;
--由pgadmin用户创建的任何表默认公开select权限
alter default privileges for role pgadmin grant select on tables to public;
--在schema为yuzhenchao上创建的任何表默认公开select权限
alter default privileges in schema yuzhenchao grant select on tables to public;
--由yuzhenchao用户创建的任何表默认公开select权限
alter default privileges for role yuzhenchao grant select on tables to public;
--在schema为copyload上创建的任何表默认公开select权限
alter default privileges in schema copyload grant select on tables to public;
--由copyload用户创建的任何表默认公开select权限
alter default privileges for role copyload grant select on tables to public;
/*
* 为了方便各用户的管理
* 需要用定义者权限创建动态sql函数
* 最终由pgadmin用户集中管理
*/
--为pgadmin用户创建sp_exec函数
create or replace function pgadmin.sp_exec(vsql varchar)
returns void --返回空
language plpgsql
security definer --定义者权限
as $function$
begin
execute vsql;
end;
$function$
;
--将对应模式的对应模式的函数给对应的模式的拥有者
alter function pgadmin.sp_exec(varchar) owner to pgadmin;
--将对应模式的sp_exec函数授权给定义者和集中用户execute权限
grant execute on function pgadmin.sp_exec(varchar) to pgadmin;
--为yuzhenchao用户创建sp_exec函数
create or replace function yuzhenchao.sp_exec(vsql varchar)
returns void --返回空
language plpgsql
security definer --定义者权限
as $function$
begin
execute vsql;
end;
$function$
;
--将对应模式的对应模式的函数给对应的模式的拥有者
alter function yuzhenchao.sp_exec(varchar) owner to yuzhenchao;
--将对应模式的sp_exec函数授权给定义者和集中用户execute权限
grant execute on function yuzhenchao.sp_exec(varchar) to yuzhenchao,pgadmin;
--为copyload用户创建sp_exec函数
create or replace function copyload.sp_exec(vsql varchar)
returns void --返回空
language plpgsql
security definer --定义者权限
as $function$
begin
execute vsql;
end;
$function$
;
--将对应模式的对应模式的函数给对应的模式的拥有者
alter function copyload.sp_exec(varchar) owner to copyload;
--将对应模式的sp_exec函数授权给定义者和集中用户execute权限
grant execute on function copyload.sp_exec(varchar) to copyload,pgadmin;
create or replace function pgadmin.sp_execsql(exec_sql character varying,exec_user character varying)
returns void
language plpgsql
security definer
as $function$
/* 作者 : v-yuzhenc
* 功能 : 集中处理程序,以某用户的权限执行某条sql语句
* exec_sql : 需要执行的sql语句
* exec_user : 需要以哪个用户的权限执行该sql语句
* */
declare
p_user varchar := exec_user;
o_search_path varchar;
begin
--记录原来的模式搜索路径
execute 'show search_path;' into o_search_path;
--临时切换模式搜索路径
execute 'SET search_path TO '||p_user||',public,oracle';
case p_user
when 'pgadmin' then perform pgadmin.sp_exec(exec_sql);
when 'yuzhenchao' then perform yuzhenchao.sp_exec(exec_sql);
when 'copyload' then perform copyload.sp_exec(exec_sql);
else raise exception '未配置该用户:%',p_user;
end case;
--恢复模式搜索路径
execute 'SET search_path TO '||o_search_path;
exception when others then
--恢复模式搜索路径
execute 'SET search_path TO '||o_search_path;
raise exception '%',sqlerrm;
end;
$function$
;
--将对应模式的对应模式的函数给对应的模式的拥有者
alter function pgadmin.sp_execsql(varchar,varchar) owner to pgadmin;
--将对应模式的sp_exec函数授权给定义者和集中用户execute权限
grant execute on function pgadmin.sp_execsql(varchar,varchar) to pgadmin;
su - postgres
#先备份全局对象
pg_dumpall -f backup.sql --globals-only
#再备份数据库
pg_dump hy_observe -Fc > hy_observe.dump
su - postgres
#先恢复全局对象
psql
\i backup.sql
--创建对应的数据库
create database hy_observe;
\q
#pg_restore进行恢复
pg_restore -d hy_observe hy_observe.dump -v
# 开启归档日志
vi $PGDATA/postgresql.conf
wal_level = replica # 或者更高级别
archive_mode = on
# backup_in_progress文件用来辅助wal日志备份,通过删除配合test指令控制wal日志备份
archive_command = 'test ! -f /usr/local/pgsql/backup_in_progress || (test ! -f /usr/local/pgsql/data/pg_archive/%f && cp %p /usr/local/pgsql/data/pg_archive/%f)'
# 重启数据库
pg_ctl restart -mf
touch /usr/local/pgsql/backup_in_progress
# 开始基础备份,可以在代码里连接数据库执行
psql -c "select pg_start_backup('hot_backup');"
# 将数据库文件进行备份
BACKUPDATE=`date '+%Y%m%d%H%m%S'`
tar -cf /data/pg_backup/pgbackup_${BACKUPDATE}.tar $PGDATA
# 结束备份,可以在代码里连接数据库执行
psql -c "select pg_stop_backup();"
# 停止wal日志备份
rm /usr/local/pgsql/backup_in_progress
# 将wal日志和基础备份打包在一起
tar -rf /data/pg_backup/pgbackup_${BACKUPDATE}.tar /usr/local/pgsql/data/pg_archive
pg_ctl stop -mf
mv $PGDATA ${PGDATA}.old
tar -xf /data/pg_backup/pgbackup_${BACKUPDATE}.tar -C $PGDATA
vi $PGDATA/recovery.conf
restore_command = 'cp /usr/local/pgsql/data/pg_archive/%f %p'
# 指定要恢复的时间点,也可以不指定,直接恢复所有数据
recovery_target_time = '2022-09-01 10:00:00'
pg_ctl start
su - postgres
#进入到数据目录
cd $PGDATA
#创建证书
openssl req -new -x509 -days 365 -nodes -text -out server.crt -keyout server.key -subj "/CN=pg_master"
#只读权限
chmod 400 server.{crt,key}
#修改pg_hba.conf
vi $PGDATA/pg_hba.conf
#所有远程连接都通过ssl连接
hostssl all postgres 0.0.0.0/0 md5
hostssl all repl 192.168.233.0/24 trust
hostssl replication repl 192.168.233.0/24 md5
hostssl all all 0.0.0.0/0 md5
#开启ssl
alter system set ssl=on;
#重新加载数据库配置
select pg_reload_conf();
#重新登录
\q
psql
#查看当前连接信息
\conninfo
#查看所有连接信息
select
pg_ssl.pid
,pg_ssl.ssl
,pg_ssl.version
,pg_sa.backend_type
,pg_sa.usename
,pg_sa.client_addr
from pg_stat_ssl pg_ssl
inner join pg_stat_activity pg_sa
on (pg_ssl.pid = pg_sa.pid);
show password_encryption;--md5
select * from pg_shadow where usename='yuzhenchao';
alter role yuzhenchao valid until '2022-12-31 23:59:59';
select * from pg_user where usename='yuzhenchao';
注意:
ls -atl $LD_LIBRARY_PATH/passwordcheck*
alter system set shared_preload_libraries=pg_stat_statements,passwordcheck;
pg_ctl restart -mf
ls -atl $LD_LIBRARY_PATH/auth_delay*
--重启生效
alter system set shared_preload_libraries=pg_stat_statements,passwordcheck,auth_delay;
pg_ctl restart -mf
--重新加载生效
alter system set auth_delay.milliseconds=5000;
--重新加载
select pg_reload_conf();
pg_ctl reload
postgresql 服务器日志
su - postgres
cd /opt
wget https://api.pgxn.org/dist/orafce/3.21.0/orafce-3.21.0.zip --no-check-certificate
unzip orafce-3.21.0.zip #解压
cd orafce-3.21.0/ #进入orafce-3.21.0目录
make clean
make #编译
make install #安装
psql -d pgtest -U pgadmin -W
create extension orafce; --创建orafce扩展
\q
官方文档:doc-html-3.2.1.tar.gz
cd /opt
wget https://github.com/Kitware/CMake/releases/download/v3.16.2/cmake-3.16.2.tar.gz
tar -zxvf cmake-3.16.2.tar.gz
cd cmake-3.16.2
./configure --prefix=/usr/local/cmake-3.16.2
make -j 4
make install
vi /etc/profile
export CMAKE_HOME=/usr/local/cmake-3.16.2
export PATH=$CMAKE_HOME/bin:$PATH
source /etc/profile
cd /opt
wget https://download.osgeo.org/geos/geos-3.11.0.tar.bz2 --no-check-certificate
tar -jxvf geos-3.11.0.tar.bz2
cd geos-3.11.0/
./configure --prefix=/usr/local/geos-3.11.0
make -j 4
make install
cd /opt
wget https://www.sqlite.org/2022/sqlite-autoconf-3390100.tar.gz --no-check-certificate
tar -zxvf sqlite-autoconf-3390100.tar.gz
cd sqlite-autoconf-3390100
vi ./sqlite3.c
#define SQLITE_CORE 1
#define SQLITE_AMALGAMATION 1
#ifndef SQLITE_PRIVATE
# define SQLITE_PRIVATE static
#endif
#define SQLITE_ENABLE_COLUMN_METADATA 1 //增加这句
./configure --prefix=/usr/local/sqlite
make -j 4
make install
mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
ln -s /usr/local/sqlite/bin/sqlite3 /usr/bin/sqlite3
sqlite3 --version
export PKG_CONFIG_PATH=/usr/local/sqlite/lib/pkgconfig:$PKG_CONFIG_PATH
cd /opt
wget http://download.osgeo.org/proj/proj-6.3.2.tar.gz
tar -zxvf proj-6.3.2.tar.gz
cd proj-6.3.2/
./configure --prefix=/usr/local/proj-6.3.2
make -j 4
make install
cd /opt
wget https://download.osgeo.org/gdal/3.2.1/gdal-3.2.1.tar.gz --no-check-certificate
tar -zxvf gdal-3.2.1.tar.gz
cd gdal-3.2.1
./configure --prefix=/usr/local/gdal-3.2.1 --with-proj=/usr/local/proj-6.3.2
make -j 4
make install
cd /opt
wget https://github.com/json-c/json-c/archive/json-c-0.13.1-20180305.tar.gz
tar -zxvf json-c-0.13.1-20180305.tar.gz
cd json-c-json-c-0.13.1-20180305
./configure --prefix=/usr/local/json-c-0.13.1
make -j 4
make install
cd /opt
wget https://mirror.ossplanet.net/gnome/sources/libxml2/2.9/libxml2-2.9.14.tar.xz --no-check-certificate
tar -xvf libxml2-2.9.14.tar.xz
cd libxml2-2.9.14
chmod +x configure
./configure --prefix=/usr/local/libxml2-2.9.14
make -j 4
make install
cd /opt
wget https://github.com/protocolbuffers/protobuf/archive/v3.10.1.tar.gz
tar -zxvf v3.10.1.tar.gz
cd protobuf-3.10.1/
./autogen.sh #自动生成configure配置文件
./configure --prefix=/usr/local/protobuf-3.10.1
make -j 4
make install
vi /etc/profile
export PROTOBUF_HOME=/usr/local/protobuf-3.10.1
export PATH=$PROTOBUF_HOME/bin:$PATH
source /etc/profile
protoc --version
libprotoc 3.10.1
cd /opt
wget https://github.com/protobuf-c/protobuf-c/releases/download/v1.3.2/protobuf-c-1.3.2.tar.gz
tar -zxvf protobuf-c-1.3.2.tar.gz
cd protobuf-c-1.3.2/
#导入protobuf的pkgconfig,否则"--No package 'protobuf' found"
export PKG_CONFIG_PATH=/usr/local/protobuf-3.10.1/lib/pkgconfig
./configure --prefix=/usr/local/protobuf-c-1.3.2
make -j 4
make install
vi /etc/profile
export PROTOBUFC_HOME=/usr/local/protobuf-c-1.3.2
export PATH=$PROTOBUFC_HOME/bin:$PATH
source /etc/profile
yum -y install boost-devel
cd /opt
wget https://github.com/CGAL/cgal/archive/releases/CGAL-4.13.tar.gz
tar -zxvf CGAL-4.13.tar.gz
cd cgal-releases-CGAL-4.13/
mkdir build && cd build
cmake ..
make
make install
cd /opt
wget https://github.com/Oslandia/SFCGAL/archive/v1.3.7.tar.gz
tar -zxvf v1.3.7.tar.gz
cd SFCGAL-1.3.7
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/sfcgal-1.3.7 ..
make -j 4
make install
vi /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/pgsql/lib
/usr/local/proj-6.3.2/lib
/usr/local/gdal-3.2.1/lib
/usr/local/geos-3.11.0/lib64
/usr/local/sfcgal-1.3.7/lib64
/usr/local/json-c-0.13.1/lib
/usr/local/libxml2-2.9.14/lib
/usr/local/protobuf-3.10.1/lib
/usr/local/protobuf-c-1.3.2/lib
ldconfig -v #重启生效
su - postgres
cd /usr/local/pgsql/contrib
wget http://download.osgeo.org/postgis/source/postgis-3.2.1.tar.gz
tar -zxvf postgis-3.2.1.tar.gz
cd postgis-3.2.1/
./configure --prefix=/usr/local/pgsql --with-gdalconfig=/usr/local/gdal-3.2.1/bin/gdal-config --with-pgconfig=/usr/local/pgsql/bin/pg_config --with-geosconfig=/usr/local/geos-3.11.0/bin/geos-config --with-projdir=/usr/local/proj-6.3.2 --with-xml2config=/usr/local/libxml2-2.9.14/bin/xml2-config --with-jsondir=/usr/local/json-c-0.13.1 --with-protobufdir=/usr/local/protobuf-c-1.3.2 --with-sfcgal=/usr/local/sfcgal-1.3.7/bin/sfcgal-config
make -j 4
make install
psql -d pgtest -U pgadmin -W
--postgis扩展
create extension postgis;
--验证栅格类数据需要的raster扩展
create extension postgis_raster;
--如果安装带有sfcgal,验证下三维sfcgal扩展
create extension postgis_sfcgal;
create extension fuzzystrmatch;
create extension postgis_tiger_geocoder;
create extension postgis_topology;
\q
could not load library "/usr/local/pgsql/lib/postgis-3.so": /usr/local/pgsql/lib/postgis-3.so: undefined symbol: GEOSLargestEmpt
ldconfig -p | grep libgeos_c.so.1
libgeos_c.so.1 (libc6,x86-64) => /usr/geos39/lib64/libgeos_c.so.1
libgeos_c.so.1 (libc6,x86-64) => /usr/local/geos-3.11.0/lib64/libgeos_c.so.1
#查找geos39
rpm -qa geos39
geos39-3.9.2-1.rhel7.x86_64
解决方案:卸载geos39-3.9.2-1.rhel7.x86_64
再次校验,发现只剩一个了
ldconfig -p | grep libgeos_c.so.1
libgeos_c.so.1 (libc6,x86-64) => /usr/local/geos-3.11.0/lib64/libgeos_c.so.1
最后 create extension postgis; 成功了
问题解决参考:PostGIS 扩展创建失败原因调查
Greenplum开发
postgresql 数据库巡检
首页 > PostgreSQL
PostgreSQL 兼容Oracle - orafce
PostGIS 扩展创建失败原因调查
PostgreSQL 数据库开发规范
PostgreSQL密码安全策略