CentOS7 安装PostgreSQL

2020-06-11 0 条评论 86 次阅读 0 人点赞

PostgreSQL安装手册

  1. 环境说明

服务器版本

[root@localhost yum.repos.d]# cat /etc/centos-release

entOS Linux release 7.6.1810 (Core)

数据库版本

PostgreSQL 10

所需文件/软件

screen

  1. 数据库安装

添加yum源

下载、安装yum源repo文件

wget https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

yum install -y pgdg-redhat-repo-latest.noarch.rpm

检查

ll /etc/yum.repos.d/

重新建立元数据缓存

yum clean all

yum makecache

安装server和客户端

需使用root 用户进行安装

yum install -y postgresql10-server postgresql10

[root@localhost ~]# yum install -y postgresql10-server postgresql10

已加载插件:fastestmirror, langpacks

Loading mirror speeds from cached hostfile

正在解决依赖关系

--> 正在检查事务

---> 软件包 postgresql10.x86_64.0.10.13-1PGDG.rhel7 将被 安装

--> 正在处理依赖关系 postgresql10-libs(x86-64) = 10.13-1PGDG.rhel7,它被软件包 postgresql10-10.13-1PGDG.rhel7.x86_64 需要

--> 正在处理依赖关系 libpq.so.5()(64bit),它被软件包 postgresql10-10.13-1PGDG.rhel7.x86_64 需要

---> 软件包 postgresql10-server.x86_64.0.10.13-1PGDG.rhel7 将被 安装

--> 正在检查事务

---> 软件包 postgresql10-libs.x86_64.0.10.13-1PGDG.rhel7 将被 安装

--> 解决依赖关系完成

依赖关系解决

======================================================================================================================================

Package                               架构                     版本                                   源                        大小

======================================================================================================================================

正在安装:

postgresql10                          x86_64                   10.13-1PGDG.rhel7                      pgdg10                   1.6 M

postgresql10-server                   x86_64                   10.13-1PGDG.rhel7                      pgdg10                   4.5 M

为依赖而安装:

postgresql10-libs                     x86_64                   10.13-1PGDG.rhel7                      pgdg10                   357 k

事务概要

======================================================================================================================================

安装  2 软件包 (+1 依赖软件包)

总下载量:6.5 M

安装大小:28 M

Downloading packages:

警告:/var/cache/yum/x86_64/7/pgdg10/packages/postgresql10-libs-10.13-1PGDG.rhel7.x86_64.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID 442df0f8: NOKEY

postgresql10-libs-10.13-1PGDG.rhel7.x86_64.rpm 的公钥尚未安装

(1/3): postgresql10-libs-10.13-1PGDG.rhel7.x86_64.rpm                                                          | 357 kB  00:00:02

(2/3): postgresql10-10.13-1PGDG.rhel7.x86_64.rpm                                                               | 1.6 MB  00:00:04

(3/3): postgresql10-server-10.13-1PGDG.rhel7.x86_64.rpm                                                                                                                                                                                                | 4.5 MB  00:00:15

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

总计                                                                                                                                                                                                                                          373 kB/s | 6.5 MB  00:00:17

file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG 检索密钥

导入 GPG key 0x442DF0F8:

用户ID     : "PostgreSQL RPM Building Project <pgsqlrpms-hackers@pgfoundry.org>"

指纹       : 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8

软件包     : pgdg-redhat-repo-42.0-11.noarch (installed)

来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

正在安装    : postgresql10-libs-10.13-1PGDG.rhel7.x86_64                                                                                                                                                                                                                1/3

正在安装    : postgresql10-10.13-1PGDG.rhel7.x86_64                                                                                                                                                                                                                     2/3

正在安装    : postgresql10-server-10.13-1PGDG.rhel7.x86_64                                                                                                                                                                                                              3/3

验证中      : postgresql10-libs-10.13-1PGDG.rhel7.x86_64                                                                                                                                                                                                                1/3

验证中      : postgresql10-10.13-1PGDG.rhel7.x86_64                                                                                                                                                                                                                     2/3

验证中      : postgresql10-server-10.13-1PGDG.rhel7.x86_64                                                                                                                                                                                                              3/3

已安装:

postgresql10.x86_64 0:10.13-1PGDG.rhel7                                                                                            postgresql10-server.x86_64 0:10.13-1PGDG.rhel7

作为依赖被安装:

postgresql10-libs.x86_64 0:10.13-1PGDG.rhel7

完毕!

[root@localhost ~]#

数据库初始化

/usr/pgsql-10/bin/postgresql-10-setup initdb

启动数据库,并检查启动情况

systemctl start postgresql-10

systemctl status postgresql-10.service

netstat -antup | grep 5432

设置开机启动

systemctl enable postgresql-10

*services文件路径:/usr/lib/systemd/system/postgresql-10.service.

修改数据库文件目录

停止服务

systemctl stop postgresql-10.service

systemctl status postgresql-10.service

创建目录

mkdir cd /app/soft/postgresql

复制文件

cp -R /var/lib/pgsql/10/ /app/soft/postgresql

调整属性

chown -R postgres: postgres /app/soft/postgresql

修改postgres用户的环境变量

vim /var/lib/pgsql/.bash_profile

注释原路径

#PGDATA=/var/lib/pgsql/10/data

添加新路径

PGDATA=/app/soft/postgresql/10/data

使之生效

source .bash_profile

修改services 文件

vim /usr/lib/systemd/system/postgresql-10.service

注释原路径

#Environment=PGDATA=/var/lib/pgsql/10/data

添加新路径

Environment=PGDATA=/data/postgres/db/10/data/

重启

systemctl daemon-reload

systemctl start postgresql-10

如果不能重新启动,执行

setenforce 0

设置postgers用户密码

su - postgres

psql

postgres=# \password

Enter new password: 输入密码

Enter it again: 输入密码

Passwords didn't match.

postgres=#\q 退出

设置远程访问

vim /app/soft/postgresql/10/data/pg_hba.conf

添加一行

host all all 0.0.0.0/0 md5

并原

host all all 127.0.0.1/32 ident

改为

host all all 127.0.0.1/32 md5

vim  /app/soft/postgresql/10/data/postgresql.conf

添加一行

listen_addresses = '*'

修改完成后重启服务

systemctl restart postgresql-10.service

防火墙根据实际情况自行处理

测试

psql -U postgres -h 192.168.242.11

  1. 常用命令

psql

使用postgers用户登陆时,可使用此命令进入数据库工作台

psql -U postgres -h 192.168.242.11

可使用此命令进入数据库工作台

CREATE USER test WITH PASSWORD 'hhx123456';

CREATE DATABASE test OWNER test;

创建用户

rpm -qa | grep postgresql

yum remove postgresql96-libs-9.6.10-1PGDG.rhel7.x86_64

卸载数据库

 

CentOS7 安装PostgreSQL

root

这个人太懒什么东西都没留下

文章评论(0)