当前位置:网站首页>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-11
4.权限设置
安装完成PostgreSQL客户端后,就会为Linux创建postgres账户,我们先对该账户设置密码。
passwd postgres
New password:
Retype new password:
分别输入“你的密码”
接着, Linux从root降级为postgres用户登陆shell终端(或者SSH),通过pgsql命令进入PG控制台,设置PG超管用户postgres的密码。
su postgres
psql
alter user postgres with password '你的密码';
exit
5.修改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-11
6.其他配置
(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/profile
PGDATA环境变量就已经全局设置了。
我们降级为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 切换数据库
边栏推荐
- pytorch 参数初始化
- 【斯坦福计网CS144项目】Lab3: TCPSender
- Route jump in wechat applet
- The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
- Jenkins远程构建项目超时的问题
- 四、高性能 Go 语言发行版优化与落地实践 青训营笔记
- Outsourcing for four years, abandoned
- 242. Bipartite graph determination
- 2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct
- [GUET-CTF2019]虚假的压缩包
猜你喜欢
知识点滴 - 关于苹果认证MFI
机器人技术创新与实践旧版本大纲
07_ Handout on the essence and practical skills of text measurement and geometric transformation
I failed in the postgraduate entrance examination and couldn't get into the big factory. I feel like it's over
[Linux] process control and parent-child processes
今日现货白银操作建议
[2022 ACTF]web题目复现
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
【斯坦福计网CS144项目】Lab4: TCPConnection
UWB learning 1
随机推荐
聊聊异步编程的 7 种实现方式
智联+影音,AITO问界M7想干翻的不止理想One
2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct
UWB learning 1
[ANSYS] learning experience of APDL finite element analysis
Kbu1510-asemi power supply special 15A rectifier bridge kbu1510
[2022 ACTF]web题目复现
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
Jenkins远程构建项目超时的问题
KBU1510-ASEMI电源专用15A整流桥KBU1510
Leetcode sword finger offer brush questions - day 20
After 95, the CV engineer posted the payroll and made up this. It's really fragrant
抽絲剝繭C語言(高階)數據的儲存+練習
idea添加类注释模板和方法模板
Outsourcing for four years, abandoned
L'externalisation a duré trois ans.
What is the difference between TCP and UDP?
按键精灵脚本学习-关于天猫抢红包
1089: highest order of factorial
1142_ SiCp learning notes_ Functions and processes created by functions_ Linear recursion and iteration