一、安装pgsql数据源
[root@server10 ~]# sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm 安装pgsql数据源
二、安装最新版本的数据库及查看pgsql数据列表
[root@server10 ~]# dnf -y install postgresql17-server postgresql17-contrib 安装pgsql17版本的数据库
[root@server10 ~]# dnf repolist 查看pgsql数据列表
repo id repo name
appstream Rocky Linux 9 - AppStream
baseos Rocky Linux 9 - BaseOS
extras Rocky Linux 9 - Extras
pgdg-common PostgreSQL common RPMs for RHEL / Rocky / AlmaLinux 9 - x86_64
pgdg12 PostgreSQL 12 for RHEL / Rocky / AlmaLinux 9 - x86_64
pgdg13 PostgreSQL 13 for RHEL / Rocky / AlmaLinux 9 - x86_64
pgdg14 PostgreSQL 14 for RHEL / Rocky / AlmaLinux 9 - x86_64
pgdg15 PostgreSQL 15 for RHEL / Rocky / AlmaLinux 9 - x86_64
pgdg16 PostgreSQL 16 for RHEL / Rocky / AlmaLinux 9 - x86_64
pgdg17 PostgreSQL 17 for RHEL / Rocky / AlmaLinux 9 - x86_64
三、初始化安装数据库及启动数据库
[root@server10 ~]# postgresql-17-setup initdb 初始化安装数据库
Initializing database ... OK
[root@server10 ~]# systemctl start postgresql-17 启动数据库
[root@server10 ~]#
[root@server10 ~]# systemctl enable postgresql-17 设置数据库开机启动
四、查看所有端口
[root@server10 ~]# netstat -tunlp 查看所有端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 816/sshd: /usr/sbin
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 2425/postgres 看这个
tcp6 0 0 :::22 :::* LISTEN 816/sshd: /usr/sbin
tcp6 0 0 ::1:5432 :::* LISTEN 2425/postgres
[root@server10 ~]# netstat -tunlp | grep postgre 查看pgsql的端口
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 2425/postgrs
tcp6 0 0 ::1:5432 :::* LISTEN 2425/postgrs
五、启动防火墙及在防火墙里添加pgsql的端口
[root@server10 ~]# systemctl enable --now firewalld 启动防火墙并设置开机启动
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.
[root@server10 ~]#
[root@server10 ~]# firewall-cmd --add-port=5432/tcp 在防火墙里添加5432端口
success
[root@server10 ~]# firewall-cmd --runtime-to-permanent 在防火墙里永久添加运行时
六、登录pgsql数据库及查看数据库配置文件的位置、查看连接信息、查看密码的加密
[root@server10 ~]# sudo -u postgres psql 登录数据库
psql (17.4)
Type "help" for help.
postgres=# show hba_file; 查看数据库配置文件的位置
hba_file
------------------------------------
/var/lib/pgsql/17/data/pg_hba.conf 这里是位置
(1 row)
postgres=# show password_encryption; 查看密码的加密
password_encryption
---------------------
scram-sha-256 加密算法 长度256
(1 row)
postgres=# \conninfo 查看连接信息
You are connected to database "postgres" as user "postgres" via socket in "/run/postgresql" at port "5432". database "postgres"数据库名称postgres user "postgres" 数据库管理员postgres
postgres=#
postgres=# \q 退出数据库
七、创建数据库用户
7.1 创建alice数据库,用户为alice
[root@server10 ~]# sudo -u postgres psql 用数据库管理员本地登录数据库
psql (17.4)
Type "help" for help.
postgres=# create user alice with createdb createrole password 'P4ssw0rdAlice'; 授权alice用户创建数据库 创建alice用户,并设置密码为P4ssw0rdAlice createrole创建角色
CREATE ROLE
postgres=# CREATE DATABASE alice OWNER alice; 创建alice数据库,用户为alice
CREATE DATABASE
7.2 创建testdb数据库,用户为testdb
postgres=# CREATE DATABASE testdb OWNER alice; 创建testdb数据库,用户为alice
CREATE DATABASE
7.3 列出数据库中所有角色
postgres=# \l 用于列出当前数据库中可用的数据库
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | IC
U Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+---
--------+-----------------------
alice | alice | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
|
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
|
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
| =c/postgres +
| | | | | | |
| postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
| =c/postgres +
| | | | | | |
| postgres=CTc/postgres
testdb | alice | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
|
(5 rows)
postgres=#\q
八、Alice用户登录数据库
[root@server10 ~]# sudo -u postgres psql -U alice -h 127.0.0.1 Alice用户登录数据库
Password for user alice: P4ssw0rdAlice
psql (17.4)
Type "help" for help.
alice=> \conninfo
You are connected to database "alice" as user "alice" on host "127.0.0.1" at port "5432".
alice=> \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | IC
U Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+---
--------+-----------------------
alice | alice | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
|
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
|
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
| =c/postgres +
| | | | | | |
| postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
| =c/postgres +
| | | | | | |
| postgres=CTc/postgres
testdb | alice | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
|
(5 rows)
九、Alice用户连接testdb数据库
9.1 Alice用户连接testdb数据库
alice=> \connect testdb; 连接testdb数据库
You are now connected to database "testdb" as user "alice".
testdb=> create table users ( 创建用户表
testdb(> id int primary key not null, id为整数类型 设置为组件,不允许为空
testdb(> name text not null, 名字为文本类型,不允许为空
testdb(> age int not null, 年龄为整数类型,不允许为空
testdb(> address char(50), 地址为字符串类型,长度为50
testdb(> salary real ); 薪水为浮点数 后边
CREATE TABLE
9.2 列出当前数据库中所有可见的表
testdb=> \dt 用于列出当前数据库中所有可见的表
List of relations
Schema | Name | Type | Owner
--------+-------+-------+-------
public | users | table | alice
(1 row)
10、向用户表中插入数据及查询、更改用户表
10.1 向用户表中插入数据
testdb=> insert into users (id,name,age,address,salary) values (1,'tom',32,'Beijing',20000.00);
INSERT 0 1 向用户表中插入数据 id=1 名字:tom 年龄:32 地址:北京 20000.00 .00 是浮点数
testdb=> insert into users (id,name,age,address,salary) values (2,'jack',26,'Shanghai',10000.00);
INSERT 0 1
testdb=> insert into users (id,name,age,address,salary) values (3,'jobs',28,'hangzhou',10000.00);
INSERT 0 1
testdb=> select * from users; 查询用户表中的所有内容
id | name | age | address | salary
----+------+-----+----------------------------------------------------+--------
1 | tom | 32 | Beijing | 20000
2 | jack | 26 | Shanghai | 10000
3 | jobs | 28 | hangzhou | 10000
(3 rows)
10.2 查询用户表
testdb=> select name,age,salary from users; 查询用户表中的名字、年龄和薪水
name | age | salary
------+-----+--------
tom | 32 | 20000
jack | 26 | 10000
jobs | 28 | 10000
(3 rows)
10.3 更改用户表
testdb=> update users set address = 'Shenzhen' where name='jobs'; 将名字是jobs用户的地址更改为深圳
UPDATE 1
testdb=> select * from users; 查询用户表的所有内容
id | name | age | address | salary
----+------+-----+----------------------------------------------------+--------
1 | tom | 32 | Beijing | 20000
2 | jack | 26 | Shanghai | 10000
3 | jobs | 28 | Shenzhen | 10000
(3 rows)
testdb=> \q
11 登录、查看数据库及删除表、数据库
11.1 登录数据库
[root@server10 ~]# sudo -u postgres psql 登录数据库
psql (17.4)
Type "help" for help.
11.2 查看数据库
postgres=# \l 查看数据库
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | IC
U Rules | Access privileges
———–+———-+———-+—————–+————-+————-+——–+—
——–+———————–
alice | alice | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
|
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
|
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
| =c/postgres +
| | | | | | |
| postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
| =c/postgres +
| | | | | | |
| postgres=CTc/postgres
testdb | alice | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
|
(5 rows)
11.3 连接testdb数据库及删除testdb数据库里的表
postgres=# \connect testdb; 连接testdb的数据库
You are now connected to database "testdb" as user "postgres".
testdb=# drop table users; 删除用户表
DROP TABLE
11.4 列出当前数据库可见的表
testdb=# \dt 列出当前数据库可见的表
Did not find any relations.
12 连接主数据库
12.1 连接主数据库
testdb=# \connect postgres 连接主数据库
You are now connected to database "postgres" as user "postgres".
12.2 删除alice及testdb数据库
postgres=# drop database alice; 删除alice数据库
DROP DATABASE
postgres=# drop database testdb; 删除testdb数据库
DROP DATABASE
12.3 查看数据库
postgres=# \l 查看数据库
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | IC
U Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+---
--------+-----------------------
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
|
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
| =c/postgres +
| | | | | | |
| postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | |
| =c/postgres +
| | | | | | |
| postgres=CTc/postgres
(3 rows)