记一次 Centos7 安装 PostgreSql 数据库-指定数据存放地址

一、基础信息说明

官网:https://www.postgresql.org/

下载说明地址:https://www.postgresql.org/download/linux/redhat/

中文社区 http://www.postgres.cn/index.php/v2/home

中文网 https://postgres.fun/

易百教程 https://www.yiibai.com/postgresql

二、系统、工具说明

1、系统版本 Centos7.4    CentOS-7-x86_64-Minimal-1804

下载地址:  http://archive.kernel.org/centos-vault/7.4.1708/isos/x86_64/   

2、VMware 版本:VMware Workstation Pro15

虚拟机安装过程可参考:https://blog.csdn.net/llwy1428/article/details/89328381

3、工具:xshell5

三、安装、部署

1、配置虚拟机网络,每台虚拟机均接入互联网

参考:

https://blog.csdn.net/llwy1428/article/details/85058028

2、设置静态IP

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33

Network 重启

[root@localhost ~]# service network restart

3、安装基本工具

[root@localhost ~]# yum install -y vim lrzsz tree wget rpm net-tools
[root@localhost ~]# yum update -y

4、浏览器打开 https://www.postgresql.org/download/linux/redhat/       (官网教程)

如下图:

5、安装资源 rpm 、客户端、服务端

[root@localhost ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
[root@localhost ~]# yum install -y postgresql11   (客户端安装)

[root@localhost ~]# yum install -y postgresql11-server   (服务端安装)

6、数据库初始化(指定目录)

创建目录、增加用户、给用户赋权

创建目录
[root@localhost ~]# mkdir /data
[root@localhost ~]# mkdir /data/pgdata
创建用户
[root@localhost ~]# useradd postgres
给创建的目录赋予 postgres 用户权限
[root@localhost ~]# chown postgres:root /data/pgdata/

7、初始化数据库

切换用户

[root@localhost ~]# su - postgres
[postgres@localhost ~]$ /usr/pgsql-11/bin/initdb -D /data/pgdata/

8、查看初始化后的数据目录

[root@localhost ~]# su - postgres   (切换用户)
[postgres@localhost ~]$ ll /data/pgdata/

9、修改配置文件

root用户下操作,将其中的 PGDATA 修改为新的数据目录:

[root@localhost ~]$ vim /usr/lib/systemd/system/postgresql-11.service

10、切换用户 postgres 修改配置文件

[root@localhost ~]# su - postgres   (切换用户)
[postgres@localhost ~]$ vim /data/pgdata/postgresql.conf

[root@localhost ~]# su - postgres   (切换用户)
[postgres@localhost ~]$ vim /data/pgdata/pg_hba.conf

host    all             all             0.0.0.0/0               md5

说明:

TYPE:pg的连接方式,local:本地unix套接字,host:tcp/ip连接

DATABASE:指定数据库

USER:指定数据库用户

ADDRESS:ip地址,可以定义某台主机或某个网段,32代表检查整个ip地址,相当于固定的ip,24代表只检查前三位,最后一                         位是0~255之间的任何一个

METHOD:认证方式,常用的有ident,md5,password,trust,reject。

                      md5是常用的密码认证方式。

                      password是以明文密码传送给数据库,建议不要在生产环境中使用。

                      trust是只要知道数据库用户名就能登录,建议不要在生产环境中使用。

                      reject是拒绝认证。

11、root 下,防火墙开启 postgresql 数据库服务的端口(如条件允许,可直接关闭防火墙)

[root@localhost ~]# firewall-cmd --add-port=5432/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success

其它防火墙操作可参考:

https://blog.csdn.net/llwy1428/article/details/99676257

12、数据库服务器开启、重启和状态查看

启动服务:
[root@localhost ~]# systemctl start postgresql-11

停止服务:
[root@localhost ~]# systemctl stop postgresql-11

重启服务:
[root@localhost ~]# systemctl restart postgresql-11

13、查看启动状态

[root@localhost ~]# netstat -lntp

14、设置数据库密码(此处我设置的密码是: 123456  )

切换用户
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ psql
psql (11.5)
Type "help" for help.

postgres=# ALTER USER postgres ENCRYPTED PASSWORD '123456';
ALTER ROLE
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

15、验证数据库的使用状态

记一次 Centos7 安装 PostgreSql 数据库-指定数据存放地址操作完毕!

安装、配置 Pgadmin 可参考:

https://hunter.blog.csdn.net/article/details/102486511

扩展:Centos7 编译安装 PostgreSql 11.4

https://blog.csdn.net/llwy1428/article/details/95444151

PostgreSql 基本操作

https://blog.csdn.net/llwy1428/article/details/102598732

参考:https://blog.csdn.net/llwy1428/article/details/102486414

猜你喜欢

转载自blog.csdn.net/llwy1428/article/details/105143053