Centos7安装配置Postgresql12

一、安装

1.1 根据操作系统选择yum仓库

https://www.postgresql.org/download/linux/redhat/

根据操作系统,选择yum仓库:


选择合适的yum仓库

这里我们选择的是Centos7:

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

1.2 yum安装

第一步:

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

第二步:
安装

yum install postgresql12-server

成功如图:


安装成功

第三步:
安装第三方依赖库:

 yum install postgresql12-libs
 yum install postgresql12-contrib
 yum install postgresql12-devel

1.3 初始化数据库

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

1.4 设置为开机启动

systemctl enable postgresql-12

1.5 启动postgresql

 systemctl start postgresql-12

1.6 设置linux用户postgres的密码

默认情况下,一旦postgresql安装完毕之后,linux会自动为postgresql数据库生成一个名为postgres的用户,我们可以修改该用户的密码。
这里我们把密码设置为:postgres

[root@iZ8vba1umdjvda1s95cyf6Z ~]# passwd postgres
Changing password for user postgres.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@iZ8vba1umdjvda1s95cyf6Z ~]#

1.7 修改数据库用户postgres的密码

在postgresql安装完成后,默认有一个数据库用户postgres,我们可以为其设置密码:首先,我们先在linux系统上,由root用户切换到postgres用户下:

[root@iZ8vba1umdjvda1s95cyf6Z ~]# su - postgres

然后执行如下命令:

psql -d template1 -c "ALTER USER postgres WITH PASSWORD 'NewPassword';"

1.8 登录数据库并查看数据库版本

以postgres用户的身份登录postgresql数据库:

登录命令: psql -U postgres

-bash-4.2$ psql -U  postgres
psql (9.2.24, server 12.3)
WARNING: psql version 9.2, server version 12.0.
         Some psql features might not work.
Type "help" for help.

postgres=#

查询数据库的版本信息:

postgres=# select version();
                                                 version                                                 
---------------------------------------------------------------------------------------------------------
 PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
(1 row)

postgres=#

1.9 配置监听参数

修改配置文件:/var/lib/pgsql/12/data/postgresql.conf

[root@iZ8vba1umdjvda1s95cyf6Z ~]# vim /var/lib/pgsql/12/data/postgresql.conf

修改为:监听所有的ip,端口号为:5432

配置监听端口号和ip

重启pg:

 systemctl restart  postgresql-12

查看运行状态:

[root@iZ8vba1umdjvda1s95cyf6Z ~]#  systemctl status postgresql-12

显示如下:

[root@iZ8vba1umdjvda1s95cyf6Z ~]#  systemctl status postgresql-12
● postgresql-12.service - PostgreSQL 12 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-06-16 14:55:49 CST; 25min ago
     Docs: https://www.postgresql.org/docs/12/static/
  Process: 28528 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 28535 (postmaster)
   CGroup: /system.slice/postgresql-12.service
           ├─28535 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
           ├─28537 postgres: logger   
           ├─28540 postgres: checkpointer   
           ├─28541 postgres: background writer   
           ├─28542 postgres: walwriter   
           ├─28543 postgres: autovacuum launcher   
           ├─28544 postgres: stats collector   
           ├─28545 postgres: logical replication launcher   
           └─31015 postgres: postgres postgres [local] idle

2.0 配置远程访问

这个pg_hba.conf文件是控制客户端认证的配置文件。(HBA stands for host-based authentication)

远程访问认证

(详情链接:https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html)

编辑配置文件:

[root@iZ8vba1umdjvda1s95cyf6Z ~]# vim /var/lib/pgsql/12/data/pg_hba.conf
image.png

修改为:

image.png

重启数据库:

[root@iZ8vba1umdjvda1s95cyf6Z ~]#  systemctl restart postgresql-12

2.1 防火墙放开5432端口

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

测试端口连通性:


[root@iZ8vba1umdjvda1s95cyf6Z ~]# nc -w 1   87.99.230.18    5432  < /dev/null && echo "tcp port ok"
tcp port ok
[root@iZ8vba1umdjvda1s95cyf6Z ~]#

到此,我们就可以用Navicat连接pg数据库了。

2.2 创建数据库和用户

2.2.1 创建一个用户并指定密码:

切换到postgres用户:

[root@iZ8vba1umdjvda1s95cyf6Z ~]# su postgres

然后登陆pg:

psql -U postgres

创建用户并指定密码:

postgres=# create user myuserwith password 'PASSWORD';
CREATE ROLE
postgres=#

2.2.2 创建数据库并指定属主

postgres=# create database  mydb  owner  myuser;

2.2.3 数据库赋权

数据库赋权,未赋权则账户只能登录控制台。

postgres=# grant all privileges on database mydb  to myuser;
GRANT
postgres=#

2.3 卸载postgresql

yum remove postgresql*

二、其他配置

2.1 时区配置

查询时区:

SHOW timezone;
image.png

设置服务器的时区

vim /var/lib/pgsql/12/data/postgresql.conf
image.png

重启:

[root@iZ8vba1umdjvda1s95cyf6Z ~]#  systemctl restart  postgresql-12

2.2 服务器编码集设置

设置默认字符集

[root@iZ8vba1umdjvda1s95cyf6Z ~]# su postgres
bash-4.2$ psql -U postgres
could not change directory to "/root"
psql (9.2.24, server 12.3)
WARNING: psql version 9.2, server version 12.0.
         Some psql features might not work.
Type "help" for help.

postgres=# initdb -E UTF8;

创建数据库时指定编码:

postgres-# create database hercules with encoding 'UTF-8' template template0 lc_collate='zh_CN.utf8' lc_ctype='zh_CN.utf8'
postgres-#

三、备份及恢复

3.1 备份

备份命令:pg_dump

pg_dump可用于将一个PostgreSQL的数据提取成一个脚本文件或归档文件。

pg_dump only dumps a single database. To back up an entire cluster, or to back up global objects that are common to all databases in a cluster (such as roles and tablespaces), use pg_dumpall.

pg_dump dbname > dumpfile

示例:

pg_dump mydb > db.sql

3.2 恢复

pg_restore命令

更多信息,请访问postgresql官网:https://www.postgresql.org

你可能感兴趣的:(Centos7安装配置Postgresql12)