centos8 安装postresql12

1.PostgreSQL的安装

1.1 下载并安装PostgreSQL官方yum源配置文件

 

 

1.2 禁用系统内置yum源的PostgreSQL安装模块

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

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

复制代码
dnf module list postgresql

dnf config-manager --disable pgdg11

dnf config-manager --disable pgdg10

dnf config-manager --disable pgdg96

dnf config-manager --disable pgdg95

dnf module disable postgresql
复制代码

 

1.3 安装PostgreSQL12的客户端和服务器端程序

 

dnf install postgresql12

dnf install postgresql12-server

dnf install postgresql12-contrib

 

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

2.PostgreSQL的配置

2.1 设置数据库实例的数据存储目录

数据库实例的默认数据存储目录是"/var/lib/pgsql/12/data/"。"/var"是一个系统目录,不宜存放大量业务数据。因此需要在初始化数据库实例之前设置数据存储目录

1)创建数据存储目录

mkdir /data/pgsql12-data

 

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

chown postgres:postgres /data/pgsql12-data

 

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

vim /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

# Location of database directory
#Environment=PGDATA=/var/lib/pgsql/12/data/
Environment=PGDATA=/data/pgsql12-data/

# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on the postmaster
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}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT


# Do not set any timeout value, so that systemd will not kill postmaster
# during crash recovery.
TimeoutSec=0

[Install]
WantedBy=multi-user.target
复制代码

 

2.2 初始化数据库实例

进入程序安装目录下的"bin"目录下,执行"postgresql-12-setup initdb"命令。

cd /usr/pgsql-12/bin

./postgresql-12-setup initdb

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

systemctl enable postgresql-12.service

systemctl start postgresql-12.service

2.4 设置数据库实例超级管理员账户"postgres"的口令

PostgreSQL12安装完成后"postgres"的默认口令为空,为空时无法使用该用户登录数据库。

复制代码
passwd postgres

su postgres

bash-4.4$ psql
psql (12.3)
Type "help" for help.

postgres=# alter user postgres with password 'gis';
ALTER ROLE
postgres=# \q
bash-4.4$ exit
复制代码

2.5 设置数据库实例的远程访问策略

PostgreSQL12安装完成后默认只允许本地访问

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

vim /data/pgsql12-data/pg_hba.conf

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

复制代码
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# 追加一条“允许全部用户,通过全部网络地址访问全部数据库”的策略
host    all             all             0.0.0.0/0               trust
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident
复制代码

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

监听地址,"*"表示全部地址,默认是"localhost"

监听端口,默认是"5432"

vim  /data/pgsql12-data/postgresql.conf
listen_addresses = '*'          # defaults to 'localhost'; use '*' for all

port = 5432                             # (change requires restart)

3)设置防火墙端口

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

firewall-cmd --zone=public --add-port=5432/tcp --permanent

firewall-cmd --reload

2.6 重新启动数据库服务实例

systemctl restart postgresql-12.service

3.数据库的运维管理

1、数据库启动、停止、重启、查看状态、开机自启动、禁用开机自启动

复制代码
# 启动数据库
systemctl start postgresql-12.service

# 停止数据库
systemctl stop postgresql-12.service

# 重启数据库
systemctl restart postgresql-12.service

# 查看数据库状态
systemctl status postgresql-12.service

# 开启数据库开机自启动
systemctl enable postgresql-12.service

# 禁用数据库开机自启动
systemctl disable postgresql-12.service
复制代码

2、初始化数据库

sudo -u postgres /usr/pgsql-12/bin/pg_ctl -D /data/pgsql12-data init

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

复制代码
systemctl status postgresql-12.service

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
复制代码

4、备份全部数据库

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

sudo -u postgres /usr/pgsql-12/bin/pg_dump -C db_name > db_bak.sql

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

sudo -u postgres /usr/pgsql-12/bin/pg_dump db_name > db_content_bak.sql

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

sudo -u postgres /usr/pgsql-12/bin/pg_dump -n "schema_name" db_name > schema_bak.sql

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

sudo -u postgres /usr/pgsql-12/bin/pg_dump -a -t "schema_name.table_name" db_name > table_content_bak.sql

5、恢复全部数据库

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

sudo -u postgres /usr/pgsql-12/bin/psql -e < db_bak.sql

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

sudo -u postgres /usr/pgsql-12/bin/psql -e db_name < db_bak.sql
原文地址:https://www.cnblogs.com/brucexl/p/15714199.html