当前位置:网站首页>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 切换数据库
边栏推荐
- Tianqing sends instructions to bypass the secondary verification
- Initial experience of teambiion network disk (Alibaba cloud network disk)
- Simple example of ros2 planning system plansys2
- Gslx680 touch screen driver source code analysis (gslx680. C)
- 《动手学深度学习》(四) -- 卷积神经网络 CNN
- 今日现货白银操作建议
- 2022-07-06:以下go语言代码是否会panic?A:会;B:不会。 package main import “C“ func main() { var ch chan struct
- Dynamics CRM server deployment - restore database prompt: the database is in use
- vus. Precautions for SSR requesting data in asyndata function
- Calculus key and difficult points record part integral + trigonometric function integral
猜你喜欢
Example of Pushlet using handle of Pushlet
身边35岁程序员如何建立起技术护城河?
Deep learning Flower Book + machine learning watermelon book electronic version I found
Simple example of ros2 planning system plansys2
海思芯片(hi3516dv300)uboot镜像生成过程详解
Outlier detection technology of time series data
Sqlmap tutorial (IV) practical skills three: bypass the firewall
Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
Flutter riverpod is comprehensively and deeply analyzed. Why is it officially recommended?
【Liunx】进程控制和父子进程
随机推荐
My ideal software tester development status
记一个并发规则验证实现
Bi she - college student part-time platform system based on SSM
Wx is used in wechat applet Showtoast() for interface interaction
IO流 file
Make a bat file for cleaning system garbage
After the interview, the interviewer roast in the circle of friends
Iterable、Collection、List 的常见方法签名以及含义
Build personal website based on flask
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
为什么要了解现货黄金走势?
UWB learning 1
微博发布案例
JS plot flot application - simple curve
Redis data migration
Summary of customer value model (RFM) technology for data analysis
leetcode:105. Constructing binary trees from preorder and inorder traversal sequences
Differences between H5 architecture and native architecture
1089: highest order of factorial
07_ Handout on the essence and practical skills of text measurement and geometric transformation