下载pg
http://www.postgres.cn/v2/download
http://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.bz2
解压
root@pg01:/opt/pg/pg/postgresql-12.2# tar jxvf postgresql-12.2.tar.bz2
配置apt源
**root@pg01:/opt/pg/pg/postgresql-12.2# cat /etc/apt/sources.list**
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
**root@pg01:/opt/pg/pg/postgresql-12.2#**
安装gcc,readline等
apt-cache search readline
apt install lib64readline8 lib64readline-dev
apt install zlib1g-dev
apt install gcc
apt install libreadline6-dev
apt install make
编译
./configure --prefix=/opt/pgsql
安装
make world && make install-world
# 如下提示证明安装完成
/usr/bin/mkdir -p '/opt/pgsql/bin'
/usr/bin/install -c vacuumlo '/opt/pgsql/bin'
make[2]: Leaving directory '/opt/pg/pg/postgresql-12.2/contrib/vacuumlo'
make[1]: Leaving directory '/opt/pg/pg/postgresql-12.2/contrib'
PostgreSQL, contrib, and documentation installation complete.
root@pg01:/opt/pg/pg/postgresql-12.2#
#说明:
# 备机不用编译,之间拷贝就行:如下
[root@wei11 opt]# tar zcf pgsql-dir.tar.gz pgsql/
[root@wei12 ~]# scp -rp wei11:/opt/pgsql-dir.tar.gz /opt/
[root@wei12 opt]# tar zxvf pgsql-dir.tar.gz
添加用户
useradd -m -d /home/postgres -s /bin/bash postgres
passwd postgres
配置postgres环境变量
export PGHOME=/opt/pgsql
export PGDATA=/opt/pgsql/data
export PGPOOL_HOME=/opt/pgpool
export PGPORT=5432
export PGUSER=postgres
export PGHOST=localhost
export PATH=$PGHOME/bin:$PGPOOL_HOME/bin:$PATH
export LD_LIBRARY_PATH=/opt/pgsql/lib
创建数据库存储目录
mkdir -p /opt/pgsql/data
chown postgres:postgres /opt/pgsql/data
初始化实例
su - postgres
initdb -D /opt/pgsql/data
修改数据库参数
vi postgresql.conf
listen_addresses = '*'
logging_collector = on
log_destination = 'csvlog'
log_truncate_on_rotation = on
log_connections = off
log_disconnections = off
log_error_verbosity = verbose
log_statement = ddl
log_min_duration_statement = 3s
log_checkpoints = on
log_lock_waits = on
deadlock_timeout = 1s
日志审计
审计是值记录用户的登陆退出以及登陆后在数据库里的行为操作,可以根据安全等级不一样设置不一样级别的审计,
此处涉及的参数文件有:
logging_collector --是否开启日志收集开关,默认off,开启要重启DB
log_destination --日志记录类型,默认是stderr,只记录错误输出
log_directory --日志路径,默认是$PGDATA/pg_log, 这个目录最好不要和数据文件的目录放在一起, 目录需要给启动postgres的操作系统用户写权限.
log_filename --日志名称,默认是postgresql-%Y-%m-%d_%H%M%S.log
log_file_mode --日志文件类型,默认为0600
log_truncate_on_rotation --默认为off,设置为on的话,如果新建了一个同名的日志文件,则会清空原来的文件,再写入日志,而不是在后面附加。
log_rotation_age --保留单个文件的最大时长,默认是1d,也有1h,1min,1s,个人觉得不实用
log_rotation_size --保留单个文件的最大尺寸,默认是10MB
log_error_verbosity --默认为default,verbose表示冗长的
log_connections --用户session登陆时是否写入日志,默认off
log_disconnections --用户session退出时是否写入日志,默认off
修改hba参数
cat >> pg_hba.conf << EOF
# IPv4 local connections:
host all all 0.0.0.0/0 md5
EOF
启动数据库
pg_ctl start
测试连接
psql -d postgres -U postgres -h 1.1.1.57 -p 5432
测试jdbc连接
jdbc:postgresql://1.1.1.57:5432/postgres
PostgresSQL创建普通用户和模式
postgres=# create user user01 with password 'user01' ;
postgres=# create database userdb owner user01; -- 创建数据库指定所属者
postgres=# grant all on database userdb to user01; -- 将dbtest所有权限赋值给user
postgres=# \c userdb ;
#下面就可以用超级用户创建,也可以用user01用户创建了:
psql -d userdb -U user01 -h 1.1.1.21 -p 5433
userdb=# create schema user01 ;
userdb=# alter schema user01 owner to user01 ;
--如果一个模式指定了owner,那么ownere建表时 默认使用该模式。
使用普通用户操作数据库
psql -d userdb -U user01 -h 1.1.1.8 -p 5432