一、rpm包安装部署
1、安装RPM包
# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# yum install -y postgresql12-server
# rpm -qa | grep postgres
postgresql12-12.4-1PGDG.rhel7.x86_64
postgresql12-libs-12.4-1PGDG.rhel7.x86_64
postgresql12-server-12.4-1PGDG.rhel7.x86_64
2、添加用户
groupadd postgres
useradd -g postgres postgres
3、配置数据、日志目录
# mkdir -p /data/pgsql/{data,logs}
# chown -R postgres:postgres /data/pgsql
4、初始化数据库
# su - postgres
su: warning: cannot change directory to /home/postgres: No such file or directory
-bash-4.2$
-bash-4.2$ /usr/pgsql-12/bin/initdb -E utf8 -D /data/pgsql/data/
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/pgsql/data ... 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/pgsql-12/bin/pg_ctl -D /data/pgsql/data/ -l logfile start
5、启动数据库
-bash-4.2$ /usr/pgsql-12/bin/postgres -D /data/pgsql/data/ > /data/pgsql/logs/postgres.log &
[1] 18534
6、创建用户和数据库
-bash-4.2$ /usr/pgsql-12/bin/psql
psql (12.4)
Type "help" for help.
postgres=# create user sansi with password '123';
CREATE ROLE
postgres=# create database db1 with encoding='utf8' owner='sansi';
CREATE DATABASE
#验证登录
-bash-4.2$ /usr/pgsql-12/bin/psql -U sansi -d db1
psql (12.4)
7、环境变量配置
$ cat /home/postgres/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
export PATH
export PGHOME=/usr/local/pgsql #软件安装目录
export PGDATA=/data/pgsql12/data #PG数据目录
$ source /home/postgres/.bash_profile
二、源码安装
1、安装源码软件包
# wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.bz2
# tar xf postgresql-12.2.tar.bz2 -C /usr/local/
2、安装依赖包
# yum install gcc gcc-c++ readline-devel readline readline-dev zlib-devel
3、编译安装
# cd /usr/local/postgresql-12.2/
# ./configure --prefix=/usr/local/pgsql
# make && make install
4、添加用户
groupadd postgres
useradd -g postgres postgres
5、配置数据、日志目录
# mkdir -p /data/pgsql12/{data,logs}
# chown -R postgres:postgres /data/pgsql12/
6、初始化数据库
# su - postgres
Last login: Tue Sep 1 22:52:40 CST 2020 on pts/0
[postgres@sansi_test ~]$ /usr/local/pgsql/
bin/ include/ lib/ share/
[postgres@sansi_test ~]$ /usr/local/pgsql/bin/initdb -D /data/pgsql12/data/
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 database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /data/pgsql12/data ... 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/pgsql/bin/pg_ctl -D /data/pgsql12/data/ -l logfile start
7、启动数据库
[postgres@sansi_test data]$ /usr/local/pgsql/bin/pg_ctl -D /data/pgsql12/data/ -l logfile start
waiting for server to start.... done
server started
8、创建用户以及相关数据库
在对数据库做初始化时会创建一个与操作系统同名的用户在数据库的超级用户,所以在改OS用户下登录数据库不需要使用账户和密码,默认使用超级用户登录,当然也可以通过修改 pg_hba.conf 配置文件进行控制。
[postgres@sansi_test data]$ /usr/local/pgsql/bin/createdb db1
[postgres@sansi_test data]$ /usr/local/pgsql/bin/createuser sansi
[postgres@sansi_test data]$ /usr/local/pgsql/bin/psql -U sansi -d db1
psql (12.2)
Type "help" for help.
db1=>
9、环境变量配置
$ cat /home/postgres/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
export PATH
export PGHOME=/usr/local/pgsql #软件安装目录
export PGDATA=/data/pgsql12/data #PG数据目录
$ source /home/postgres/.bash_profile
三、PG数据库关闭的三种模式(smart/fast/immediate)
语法
pg_ctl stop -D ${datadir} -m smart/fast/immediate
1、 Smart Shutdown
在接收到关机命令后,服务器将不再接受新的链接请求,并且等待当前已建立的连接结束。
若数据库正处于联机备份模式,需要等待联机备份模式不再活跃后关闭。此时允许超级用户的连接,主要是为了超级用户连接上去终止连接备份模式。
若数据库正处于恢复状态,会等待恢复和流复制的会话终止后进行关闭。
该模式更偏向于对数据库服务器进行维护前的关闭,一般使用较少,因为在该模式下需要用户主动断开连接后才会开始关闭数据库的动作。
/usr/pgsql-12/bin/pg_ctl stop -D /data/pgsql/data/ -m smart
waiting for server to shut down.... done
server stopped
2、Fast Shutdown
默认关闭模式,快速关机模式。主进程postmaster不允许新连接并发送所有现有服务进程发送SIGINT信号,让它们中止当前事务并立即退出。等待所有服务进程退出后关闭数据库。如果服务器处于联机备份模式,则备份模式将终止,从而使备份无效。
SIGINT “Fast” 模式不会等待客户端断开连接并且将终止进行中的在线备份。所有活动事务都被回滚并且客户端被强制断开连接,然后服务器被关闭。
3、Immediate Shutdown
这是立即关机模式。主进程postmaster将SIGQUIT发送到所有子进程并立即退出,所有子进程也会立即退出,而不执行正常的数据库关闭处理。这将导致在下次启动时恢复(通过重放WAL日志)。建议仅在紧急情况下使用。