当前位置:网站首页>CentOS7下安装PostgreSQL11数据库
CentOS7下安装PostgreSQL11数据库
2022-07-07 04:16:00 【守护石】
1.获取PG的Repo文件
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm我们通过如下命令就可以知道repo文件的安装目录:
[[email protected] ~]# rpm -ql pgdg-redhat-repo
/etc/pki/rpm-gpg
/etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
/etc/yum.repos.d/pgdg-redhat-all.repo
pgdg-redhat-all.repo文件为yum远程安装提供了PostgreSQL的远程安装源。
2.安装PostgreSQL客户端和服务端
yum install postgresql11 postgresql11-server一路y键就可以了。
3.初始化
/usr/pgsql-11/bin/postgresql-11-setup initdb接着将postgresql服务加入自启动并启动PostgreSQL
systemctl enable postgresql-11
systemctl start postgresql-114.权限设置
安装完成PostgreSQL客户端后,就会为Linux创建postgres账户,我们先对该账户设置密码。
passwd postgresNew password:
Retype new password:
分别输入“你的密码”
接着, Linux从root降级为postgres用户登陆shell终端(或者SSH),通过pgsql命令进入PG控制台,设置PG超管用户postgres的密码。
su postgres
psql
alter user postgres with password '你的密码';
exit5.修改PG相关配置文件,设定外部访问控制规则
首先配置postgresql.conf文件,开放PG网络访问端口。
vi /var/lib/pgsql/11/data/postgresql.conf修改listen_addresses的值为‘*’:
listen_addresses = '*' # what IP address(es) to listen on;其次配置pg_hba.conf文件,细粒度控制PG服务访问和PG主从复制的IP段
vi /var/lib/pgsql/11/data/pg_hba.conf重点关注下面配置行尾部是“md5”的配置项的修改:
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 192.168.83.0/16 md5
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all md5
host replication all 127.0.0.1/32 md5
host replication all 192.168.83.0/24 md5
host replication all ::1/128 ident上面配置中PostgreSQL数据库服务的访问IP配置是192.168.83.0/16 ,掩码位是16,那么可以访问的IP段为192.168.0.1~192.168.255.254。
PostgreSQL服务的复制(replication)访问IP配置是 192.168.83.0/24,掩码位是24,那么可以访问的IP段为192.168.83.1~192.168.83.254。
注:如果你想完全放开PostgreSQL的访问控制,只需要将192.168.83.0/16改为0.0.0.0/0。
最后我们需要重返root用户,重启PG服务,让配置生效。
systemctl restart postgresql-116.其他配置
(1) Linux重新降级登陆postgres用户,然后使用pgsql命令进入PG控制台,我们看看PostgreSQL是否是中文环境。
select * from pg_database;我们重点关注datcollate,datctype两列,看看是否是zh_CN.UTF-8
postgres=# select * from pg_database;
datname | datdba | encoding | datcollate | datctype | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace |
datacl
-----------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+------------+---------------+--------------
-----------------------
postgres | 10 | 6 | zh_CN.UTF-8 | zh_CN.UTF-8 | f | t | -1 | 13880 | 561 | 1 | 1663 |
template1 | 10 | 6 | zh_CN.UTF-8 | zh_CN.UTF-8 | t | t | -1 | 13880 | 561 | 1 | 1663 | {=c/postgres,
postgres=CTc/postgres}
template0 | 10 | 6 | zh_CN.UTF-8 | zh_CN.UTF-8 | t | f | -1 | 13880 | 561 | 1 | 1663 | {=c/postgres,
postgres=CTc/postgres}
(3 rows)如若不是,执行SQL修改:
update pg_database set datcollate='zh_CN.UTF-8',datctype='zh_CN.UTF-8';(2) 让postgres用户可以重新加载pg_hba.conf配置文件:
重返root用户,连续执行如下命令:
echo 'export PGDATA=/var/lib/pgsql/11/data' >> /etc/profile
source /etc/profilePGDATA环境变量就已经全局设置了。
我们降级为postgres用户,若去修改pg_hba.conf配置文件,直接执行命令:
/usr/pgsql-11/bin/pg_ctl reload就可以重新加载pg_hba.conf配置文件,而无需重启postgresql-11服务。
7.PostgreSQL命令简单描述
(1).创建数据库,一定要指定角色(PG主要以角色来控制)。
例如,我们首先通过pgsql,使用了postgres超级用户登陆:
create role health login password '你的密码';
create database health_db with owner= health;我们创建了角色“health”,然后再创建数据库“health_db”的时候,需要指定这个数据库的所有者为“health”。
对应的删除数据库和角色命令:
drop database health_db;
drop role health;当要全面操作“health_db”数据库的时候,请退出PG控制台,进入到Linux的postgres用户的登陆状态,再用“health”角色登陆PG控制台:
psql -U health -d health_db我们可以通过如下命令:
\l 或者\l databaseName 查看数据库
\d 或者 \d tableName 查看数据表
\du 查看用户
\dnS 查看模式
\c databaseName 切换数据库边栏推荐
猜你喜欢

Kbu1510-asemi power supply special 15A rectifier bridge kbu1510

07_ Handout on the essence and practical skills of text measurement and geometric transformation

Advanced practice of C language (high level) pointer

How to * * labelimg

1090: integer power (multi instance test)

KBU1510-ASEMI电源专用15A整流桥KBU1510

通信设备商,到底有哪些岗位?

Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp

Sqlmap tutorial (IV) practical skills three: bypass the firewall
![[SUCTF 2019]Game](/img/9c/362117a4bf3a1435ececa288112dfc.png)
[SUCTF 2019]Game
随机推荐
Blue Bridge Cup Birthday candles (violence)
Talk about seven ways to realize asynchronous programming
@component(““)
外包干了三年,废了...
深度学习花书+机器学习西瓜书电子版我找到了
抽絲剝繭C語言(高階)指針的進階
[2022 CISCN]初赛 web题目复现
idea添加类注释模板和方法模板
Resource create package method
微信小程序中使用wx.showToast()进行界面交互
Causes and solutions of oom (memory overflow)
今日现货白银操作建议
Jenkins remote build project timeout problem
Docker compose start redis cluster
[SUCTF 2019]Game
[GUET-CTF2019]虚假的压缩包
3、 High quality programming and performance tuning practical youth training camp notes
Tianqing sends instructions to bypass the secondary verification
Invalid table alias or column reference`xxx`
About some details of final, I have something to say - learn about final CSDN creation clock out from the memory model