CentOS8安装PostgreSQL12
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了CentOS8安装PostgreSQL12,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5124字,纯文字阅读大概需要8分钟。
内容图文
1.PostgreSQL的安装
1.1 下载并安装PostgreSQL官方yum源配置文件
dnf installhttps://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
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/gispathfinder/p/13054284.html
内容总结
以上是互联网集市为您收集整理的CentOS8安装PostgreSQL12全部内容,希望文章能够帮你解决CentOS8安装PostgreSQL12所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。