RedHat/CentOS8【PostgreSQL12】安装、配置和管理

PostgreSQL12是一款功能强大且开源(遵循BSD协议)的关系型数据库管理系统(RDBMS)。PostgreSQL12兼具NoSQL特性,支持两种JSON数据类型:json和jsonb,并自带流复制的实时备份和读写分离功能。

本方案基于CentOS8系统设计,建议在RedHat/CentOS系统中使用。


1.PostgreSQL的安装

1、打开PostgreSQL官方网站下载页面【https://www.postgresql.org/download/linux/】,选择PostgreSQL适配的操作系统版本。

PostgreSQL适配操作系统

PostgreSQL适配操作系统

2、选择PostgreSQL版本,适配平台和架构。

PostgreSQL版本,适配平台和架构

PostgreSQL版本,适配平台和架构

3、下载并安装PostgreSQL官方yum源配置文件。

[centos@host ~]$ sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

技巧:PostgreSQL官方的yum源配置文件提供了PostgreSQL12/11/10/9.6/9.5/9.4共6个版本的配置信息,一般情况下我们只使用计划安装版本的配置信息,禁用不需要的配置信息可以提高下载速度。本例计划安装12版,可以禁用11/10/9.6/9.5/9.4版的配置信息。

[centos@host  ~]$ sudo dnf config-manager --disable pgdg11

[centos@host  ~]$ sudo dnf config-manager --disable pgdg10

[centos@host  ~]$ sudo dnf config-manager --disable pgdg96

[centos@host  ~]$ sudo dnf config-manager --disable pgdg95

[centos@host  ~]$ sudo dnf config-manager --disable pgdg94

注意:如果使用本地或私有yum源,可忽略第1-3步,直接获取本地/私有yum源的配置文件使用即可。有关如何进行yum源的本地化/私有化,请阅读文章《RedHat/CentOS8 【国内/本地/私有 YUM 源】制作和使用》,文章地址【https://www.jianshu.com/p/68db74388600】。

4、禁用系统内置yum源的PostgreSQL安装模块。

CentOS8的内置yum源中已经提供PostgreSQL安装模块(但比官方提供的版本要旧),而在执行安装命令时,内置yum源的优先级高于其他yum源,因此要禁用内置yum源的PostgreSQL安装模块。

[centos@host  ~]$ sudo dnf module disable postgresql

5、安装PostgreSQL12的客户端和服务器端程序。

[centos@host  ~]$ sudo dnf install postgresql12

[centos@host  ~]$ sudo dnf install postgresql12-server

[centos@host  ~]$ sudo dnf install postgresql12-contrib

注意:程序安装目录是"/usr/pgsql-12",程序运行目录是"/var/run/postgresql",程序运行用户和组是"postgres:postgres","postgres"用户和组安装时默认创建。



2.PostgreSQL的配置

1、设置数据库实例的数据存储目录。

数据库实例的默认数据存储目录是"/var/lib/pgsql/12/data/"。"/var"是一个系统目录,不宜存放大量业务数据,通常的解决方案是在系统中挂载一块数据盘,专门用于存储业务数据。因此需要在初始化数据库实例之前设置数据存储目录

假设本例中使用"/data"目录挂载数据盘,具体操作如下:

1)创建数据存储目录。

[centos@host  ~]$ sudo mkdir /data/pgsql12-data

2)设置数据存储目录的所有者用户和组为"postgres:postgres","postgres"用户和组在安装PostgreSQL12时已创建。

[centos@host  ~]$ sudo chown postgres:postgres /data/pgsql12-data

3)修改PostgreSQL12开机启动服务配置文件,设置为新的数据存储目录。

使用文本编辑器打开"/usr/lib/systemd/system/postgresql-12.service"文件:

[centos@host  ~]$ sudo gedit /usr/lib/systemd/system/postgresql-12.service

修改配置文件中的"Environment"参数项并保存。

[Unit]

Description=PostgreSQL 12 database server

Documentation=https://www.postgresql.org/docs/12/static/

After=syslog.target

After=network.target

[Service]

Type=notify

User=postgres

Group=postgres

Environment=PGDATA=/data/pgsql12-data

OOMScoreAdjust=-1000

Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj

Environment=PG_OOM_ADJUST_VALUE=0

ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA}

ExecStart=/usr/pgsql-12/bin/postmaster -D ${PGDATA}

ExecStop=/bin/kill -HUP $MAINPID

ExecReload=/bin/kill -HUP $MAINPID

KillMode=mixed

KillSignal=SIGINT

TimeoutSec=0

[Install]

WantedBy=multi-user.target

2、初始化数据库实例。进入程序安装目录下的"bin"目录下,执行"postgresql-12-setup initdb"命令。

[centos@host  ~]$ cd /usr/pgsql-12/bin

[centos@host  bin]$ sudo ./postgresql-12-setup initdb

3、启动数据库实例服务,并设置为开机自动启动。

[centos@host  ~]$ sudo systemctl enable postgresql-12.service

[centos@host  ~]$ sudo systemctl start postgresql-12.service

4、设置数据库实例超级管理员账户"postgres"的口令。PostgreSQL12安装完成后"postgres"的默认口令为空,为空时无法使用该用户登录数据库。

[centos@host  ~]$ sudo passwd postgres

[centos@host  ~]$ su postgres

bash-4.4$ psql

postgres=# alter user postgres with password 'password';

postgres=# \q

bash-4.4$ exit

5、设置数据库实例的远程访问策略。PostgreSQL12安装完成后默认只允许本地访问。

1)设置数据库实例访问策略,可以设置多个由主机类型、数据库、用户、IP地址组成的策略。

使用文本编辑器打开数据存储目录下的"pg_hba.conf"文件:

[centos@host  ~]$ sudo gedit /data/pgsql12-data/pg_hba.conf

在文件的"# IPv4 local connections"策略中追加一条“允许全部用户,通过全部网络地址访问全部数据库”的策略并保存,策略定义如下:

# IPv4 local connections:

# 追加一条“允许全部用户,通过全部网络地址访问全部数据库”的策略

host      all      all    0.0.0.0/0    trust

2)设置数据库实例监听地址和端口。

使用文本编辑器打开数据存储目录下的"postgresql.conf"文件:

[centos@host ~]$ sudo gedit /data/pgsql12-data/postgresql.conf

修改文件中的"listen_addresses"(监听地址,"*"表示全部地址,默认是"localhost")和"port"(监听端口,默认是"5432")。设置为"监听服务器的所有IP地址,使用默认端口"的策略并保存,策略定义如下:

# 设置监听服务器的所有IP地址

listen_addresses = '*'

# 设置端口号,默认使用端口5432

port = 5432

3)设置防火墙端口(CentOS8默认安装firewall防火墙),允许"5432"端口(PostgreSQL默认端口)访问服务器。

[centos@host ~]$ sudo firewall-cmd --zone=public --add-port=5432/tcp --permanent

[centos@host ~]$ sudo firewall-cmd --reload

6、重新启动数据库服务实例。

[centos@host ~]$ sudo systemctl restart postgresql-12.service

在多个实例并行的情况下,每个实例独立数据存储目录、独立端口、独立启动服务。


3.数据库的运维管理

1、启动数据库(任选一种方式)

[centos@host ~]$ sudo systemctl start postgresql-12.service

或者

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/pg_ctl -D /data/pgsql12-data start

2、停止数据库(任选一种方式)

[centos@host ~]$ sudo systemctl stop postgresql-12.service

或者

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/pg_ctl -m fast -D /data/pgsql12-data stop

3、重启数据库(任选一种方式)

[centos@host ~]$ sudo systemctl restart postgresql-12.service

或者

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/pg_ctl -D /data/pgsql12-data restart

4、查看数据库状态(任选一种方式)

[centos@host ~]$ sudo systemctl status postgresql-12.service

或者

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/pg_ctl -D /data/pgsql12-data status

5、开启数据库开机自启动

[centos@host ~]$ sudo systemctl enable postgresql-12.service

6、禁用数据库开机自启动

[centos@host ~]$ sudo systemctl disable postgresql-12.service

7、初始化数据库

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/pg_ctl -D /data/pgsql12-data init

8、将从属节点提升为主要节点

[centos@host ~]$ sudo systemctl status postgresql-12.service

[centos@host  ~]$ su postgres

bash-4.4$ rm -rf /data/pgsql12-data/*

bash-4.4$ /usr/pgsql-12/bin/pg_ctl -D /data/pgsql12-data promote

bash-4.4$ exit

9、备份全部数据库

1)备份数据库(包含创建数据库)

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/pg_dump -C db_name > db_bak.sql

2)备份数据库内容(不含创建数据库)

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/pg_dump db_name > db_content_bak.sql

3)备份数据库架构(命名空间/模式)和内容(包含创建数据库架构)

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/pg_dump -n "schema_name" db_name > schema_bak.sql

4)备份表内数据(不含创建表结构)

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/pg_dump -a -t "schema_name.table_name" db_name > table_content_bak.sql

10、恢复全部数据库

1)恢复数据库及其内容(数据库不存在)

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/psql -e < db_bak.sql

2)恢复数据库内容(数据库必须已存在,且库中不存在备份文件中将要的创建的对象)

[centos@host ~]$ sudo -u postgres /usr/pgsql-12/bin/psql -e db_name < db_bak.sql

你可能感兴趣的:(RedHat/CentOS8【PostgreSQL12】安装、配置和管理)