当前位置:网站首页>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 切换数据库
边栏推荐
- 普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
- ASEMI整流桥RS210参数,RS210规格,RS210封装
- How to reduce inventory with high concurrency on the Internet
- 直播平台源码,可折叠式菜单栏
- KBU1510-ASEMI电源专用15A整流桥KBU1510
- Jenkins远程构建项目超时的问题
- Detailed explanation of neo4j installation process
- 一、Go知识查缺补漏+实战课程笔记 | 青训营笔记
- 毕设-基于SSM大学生兼职平台系统
- [Linux] process control and parent-child processes
猜你喜欢
MIPS uclibc cross compile ffmpeg, support g711a encoding and decoding
Stockage et pratique des données en langage C (haut niveau)
Calculus key and difficult points record part integral + trigonometric function integral
ROS2规划系统plansys2简单的例子
Leetcode-543. Diameter of Binary Tree
[UTCTF2020]file header
Talk about seven ways to realize asynchronous programming
Jenkins远程构建项目超时的问题
Model application of time series analysis - stock price prediction
《动手学深度学习》(四) -- 卷积神经网络 CNN
随机推荐
buuctf misc USB
IO stream file
Simple example of ros2 planning system plansys2
海思芯片(hi3516dv300)uboot镜像生成过程详解
Blue Bridge Cup Netizen age (violence)
Solution: could not find kf5 (missing: coreaddons dbusaddons doctools xmlgui)
Blue Bridge Cup Birthday candles (violence)
《动手学深度学习》(四) -- 卷积神经网络 CNN
3、 High quality programming and performance tuning practical youth training camp notes
三、高质量编程与性能调优实战 青训营笔记
【经验分享】如何为visio扩展云服务图标
4、 High performance go language release optimization and landing practice youth training camp notes
Docker compose start redis cluster
[Linux] process control and parent-child processes
Stockage et pratique des données en langage C (haut niveau)
Differences between H5 architecture and native architecture
About some details of final, I have something to say - learn about final CSDN creation clock out from the memory model
抽丝剥茧C语言(高阶)数据的储存+练习
【云原生】内存数据库如何发挥内存优势
Mutual conversion between InputStream, int, shot, long and byte arrays