[root@dns ~]# ls -al /mnt/cdrom/
總用量 53
dr-xr-xr-x. 7 root root 2048 4月 4 2020 .
drwxr-xr-x. 4 root root 31 10月 31 14:54 ..
dr-xr-xr-x. 4 root root 2048 4月 4 2020 AppStream
dr-xr-xr-x. 4 root root 2048 4月 4 2020 BaseOS
-r--r--r--. 1 root root 60 4月 4 2020 .discinfo
dr-xr-xr-x. 3 root root 2048 4月 4 2020 EFI
-r--r--r--. 1 root root 8266 4月 4 2020 EULA
-r--r--r--. 1 root root 1455 4月 4 2020 extra_files.json
-r--r--r--. 1 root root 18092 4月 4 2020 GPL
dr-xr-xr-x. 3 root root 2048 4月 4 2020 images
dr-xr-xr-x. 2 root root 2048 4月 4 2020 isolinux
-r--r--r--. 1 root root 103 4月 4 2020 media.repo
-r--r--r--. 1 root root 1669 4月 4 2020 RPM-GPG-KEY-redhat-beta
-r--r--r--. 1 root root 5134 4月 4 2020 RPM-GPG-KEY-redhat-release
-r--r--r--. 1 root root 1796 4月 4 2020 TRANS.TBL
-r--r--r--. 1 root root 1560 4月 4 2020 .treeinfo
[root@dns ~]#
[root@dns ~]#
用命令“ rpm -qa | grep postgresql”,發(fā)現(xiàn)沒有安裝
[root@dns ~]#
[root@dns ~]# rpm -qa |grep postgresql
[root@dns ~]#
[root@dns ~]#
用命令” dnf clean all“,清除緩存。
用命令” dnf install @postgresql -y“,安裝
[root@dns ~]# dnf install @postgresql -y
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元數(shù)據(jù)過期檢查:0:00:56 前,執(zhí)行于 2022年12月01日 星期四 08時10分25秒。
依賴關(guān)系解決。
================================================================================
軟件包 架構(gòu) 版本 倉庫 大小
================================================================================
安裝組/模塊包:
postgresql-server x86_64 10.6-1.module+el8+2469+5ecd5aae AppStream 5.1 M
安裝依賴關(guān)系:
libpq x86_64 12.1-3.el8 AppStream 195 k
postgresql x86_64 10.6-1.module+el8+2469+5ecd5aae AppStream 1.5 M
安裝模塊配置檔案:
postgresql/server
啟用模塊流:
postgresql 10
事務(wù)概要
================================================================================
安裝 3 軟件包
總計(jì):6.7 M
安裝大?。?7 M
下載軟件包:
運(yùn)行事務(wù)檢查
事務(wù)檢查成功。
運(yùn)行事務(wù)測試
事務(wù)測試成功。
運(yùn)行事務(wù)
準(zhǔn)備中 : 1/1
安裝 : libpq-12.1-3.el8.x86_64 1/3
安裝 : postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64 2/3
運(yùn)行腳本: postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64 3/3
安裝 : postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64 3/3
運(yùn)行腳本: postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64 3/3
驗(yàn)證 : libpq-12.1-3.el8.x86_64 1/3
驗(yàn)證 : postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64 2/3
驗(yàn)證 : postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64 3/3
Installed products updated.
已安裝:
libpq-12.1-3.el8.x86_64
postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64
postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64
完畢!
[root@dns ~]#
再次查看是否已安裝PostgreSQL
[root@dns ~]# rpm -qa |grep postgresql
postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64
postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64
[root@dns ~]#
需要先初始化PostgreSQL數(shù)據(jù)庫,才能啟動。
使用命令:postgresql-setup initdb
[root@dns ~]# postgresql-setup initdb
WARNING: using obsoleted argument syntax, try --help
WARNING: arguments transformed to: postgresql-setup --initdb --unit postgresql
* Initializing database in '/var/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
[root@dns ~]#
啟動PostgreSQL數(shù)據(jù)庫
[root@dns ~]# systemctl start postgresql
[root@dns ~]#
查看PostgreSQL數(shù)據(jù)庫狀態(tài)
[root@dns ~]# systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor>
Active: active (running) since Thu 2022-12-01 08:16:25 EST; 15s ago
Process: 5493 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (c>
Main PID: 5496 (postmaster)
Tasks: 8 (limit: 11160)
Memory: 20.2M
CGroup: /system.slice/postgresql.service
├─5496 /usr/bin/postmaster -D /var/lib/pgsql/data
├─5497 postgres: logger process
├─5500 postgres: checkpointer process
├─5501 postgres: writer process
├─5502 postgres: wal writer process
├─5503 postgres: autovacuum launcher process
├─5504 postgres: stats collector process
└─5505 postgres: bgworker: logical replication launcher
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa systemd[1]: Starting Pos>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa systemd[1]: Started Post>
[root@dns ~]#
設(shè)置PostgreSQL開機(jī)自啟動
[root@dns ~]# systemctl enable postgresql
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/systemd/system/postgresql.service.
[root@dns ~]#
登錄PostgreSQL。
PostgreSQL默認(rèn)只能用預(yù)定義的數(shù)據(jù)庫超級用戶postgres的身份登錄,
而不能用root用戶登錄。
使用命令:su postgres 切換到用戶postgres
[root@dns ~]# su - postgres
[postgres@dns ~]$
[postgres@dns ~]$
[postgres@dns ~]$
使用命令:psql 進(jìn)入到psql環(huán)境。
提示符是“postgres”,意思是當(dāng)前數(shù)據(jù)庫是postgres
[postgres@dns ~]$ psql
psql (10.6)
輸入 "help" 來獲取幫助信息.
postgres=#
postgres=#
)在psql環(huán)境下,可以執(zhí)行psql元命令。所謂元命令就是由反斜杠開頭,后加若干字母組成的命令。
用“\l”元命令,列出服務(wù)器上的所有數(shù)據(jù)庫
postgres=# \l
數(shù)據(jù)庫列表
名稱 | 擁有者 | 字元編碼 | 校對規(guī)則 | Ctype | 存取權(quán)限
-----------+----------+----------+-------------+-------------+------------------
-----
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/post
gres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/post
gres
(3 行記錄)
postgres=#
用“\c template1”元命令,切換當(dāng)前數(shù)據(jù)庫為template1
postgres=# \c template1
您現(xiàn)在已經(jīng)連接到數(shù)據(jù)庫 "template1",用戶 "postgres".
template1=#
在psql環(huán)境下,可以使用SQL語句。
使用“create database db1;”創(chuàng)建數(shù)據(jù)庫db1
注意一定要加分號。
template1=# create database db1;
CREATE DATABASE
template1=# \l
數(shù)據(jù)庫列表
名稱 | 擁有者 | 字元編碼 | 校對規(guī)則 | Ctype | 存取權(quán)限
-----------+----------+----------+-------------+-------------+-----------------------
db1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 行記錄)
template1=#
用“\c db1”元命令,切換當(dāng)前數(shù)據(jù)庫為db1
template1=# \c db1
您現(xiàn)在已經(jīng)連接到數(shù)據(jù)庫 "db1",用戶 "postgres".
db1=#
用“create table employee”創(chuàng)建表employee。
三個字段:編號,姓名,性別
db1=# create table employee(
db1(# no char(20),
db1(# name char(30),
db1(# gender char(2)
db1(# );
錯誤: 關(guān)系 "employee" 已經(jīng)存在
db1=#
用“select * from employee;”查看表employee的字段
db1=# select * from employee;
no | name | gender
----+------+--------
(0 行記錄)
db1=# ^C
用insert語句向 employee表添加兩條記錄
db1=# insert into employee values
db1-# ('0002','Tom','M');
INSERT 0 1
db1=# insert into employee values
db1-# ('0002','Tom','M');
INSERT 0 1
db1=# ^C
db1=# select * from employee;
no | name | gender
----------------------+--------------------------------+--------
0002 | Tom | M
0001 | Alice | F
(2 行記錄)
db1=#
用drop table語句 刪除 employee表
db1=# drop table employee;
“\d”元命令,發(fā)現(xiàn)db1數(shù)據(jù)庫中沒有表了
由于我并沒有執(zhí)行刪除命令所以還是可以看到數(shù)據(jù)表的
db1=# \d
關(guān)聯(lián)列表
架構(gòu)模式 | 名稱 | 類型 | 擁有者
----------+----------+--------+----------
public | employee | 數(shù)據(jù)表 | postgres
(1 行記錄)
db1=#
“\c postgres”元命令,切換到postgres數(shù)據(jù)庫
db1=# \c postgres
您現(xiàn)在已經(jīng)連接到數(shù)據(jù)庫 "postgres",用戶 "postgres".
postgres=#
“ 用drop database語句 刪除 數(shù)據(jù)庫db1
postgres=# drop database db1;
DROP DATABASE
“\ q”元命令,退出數(shù)據(jù)庫
postgres=# \q
[postgres@dns ~]$
使用命令:ss -nlt查看服務(wù)器端口號5432,正在監(jiān)聽本機(jī)
[postgres@dns ~]$ ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 10 192.168.10.1:53 0.0.0.0:*
LISTEN 0 10 192.168.122.1:53 0.0.0.0:*
LISTEN 0 10 127.0.0.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
(5432是postgreSQL的端口)
LISTEN 0 128 127.0.0.1:5432 (5432是postgreSQL的端口) 0.0.0.0:*
(5432是postgreSQL的端口)
LISTEN 0 128 127.0.0.1:953 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 10 [::1]:53 [::]:*
LISTEN 0 32 *:21 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 [::1]:5432 [::]:*
LISTEN 0 128 [::1]:953 [::]:*
[postgres@dns ~]$
配置客戶端遠(yuǎn)程訪問PostgreSQL數(shù)據(jù)庫
postgres用戶本來沒有密碼,現(xiàn)設(shè)置postgres用戶密碼
[postgres@dns ~]$ psql
psql (10.6)
輸入 "help" 來獲取幫助信息.
postgres=# \password postgres
輸入新的密碼:
再次輸入:
postgres=#
#推出psql
postgres=# \q
編輯主配置文件/var/lib/pgsql/data/postgresql.conf,
使PostgreSQL服務(wù)器能夠在所有可用網(wǎng)絡(luò)上偵聽。默認(rèn)情況是只有本機(jī)
可以偵聽。
先對配置文件做備份,再修改。
[postgres@dns ~]$ cd /var/lib/pgsql/data/
[postgres@dns data]$
[postgres@dns data]$ ls
base pg_hba.conf pg_replslot pg_tblspc postgresql.conf
current_logfiles pg_ident.conf pg_serial pg_twophase postmaster.opts
global pg_log pg_snapshots PG_VERSION postmaster.pid
log pg_logical pg_stat pg_wal
pg_commit_ts pg_multixact pg_stat_tmp pg_xact
pg_dynshmem pg_notify pg_subtrans postgresql.auto.conf
[postgres@dns data]$ cp postgresql.conf postgresql.conf.bak
[postgres@dns data]$
找到這一行:#listen_addresses =‘localhost’
修改為:listen_addresses =‘’
把#號去掉,localhost改為,意思是任意IP都可以偵聽
改好后存盤退出
[postgres@dns data]$ vim postgresql.conf
[postgres@dns data]$
重啟postgresql
[postgres@dns data]$ systemctl restart postgresql
[postgres@dns data]$
[postgres@dns data]$
再查看postgersql的
現(xiàn)在就改為0.0.0.0地址監(jiān)聽5432了
[postgres@dns data]$ ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 10 192.168.10.1:53 0.0.0.0:*
LISTEN 0 10 192.168.122.1:53 0.0.0.0:*
LISTEN 0 10 127.0.0.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
(5432是postgreSQL的端口)
LISTEN 0 128 0.0.0.0:5432 0.0.0.0:*
(5432是postgreSQL的端口)
LISTEN 0 128 127.0.0.1:953 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 10 [::1]:53 [::]:*
LISTEN 0 32 *:21 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 [::]:5432 [::]:*
LISTEN 0 128 [::1]:953 [::]:*
[postgres@dns data]$
啟用MD5加密的密碼身份驗(yàn)證
先對配置文件pg_hba.conf做備份,再修改。
[postgres@dns data]$ cp pg_hba.conf pg_hba.conf.bak
[postgres@dns data]$
[postgres@dns data]$ echo "host all all 0.0.0.0/0 md5">>pg_hba.conf
[postgres@dns data]$
===================================================
===================================================
在防火墻上允許數(shù)據(jù)庫服務(wù)
[postgres@dns data]$ firewall-cmd --permanent --add-service=postgresql
success
[postgres@dns data]$
[postgres@dns data]$ firewall-cmd --reload
success
[postgres@dns data]$
然后到客戶機(jī)上驗(yàn)證連接
[root@localhost ~]# df -l
文件系統(tǒng) 1K-塊 已用 可用 已用% 掛載點(diǎn)
devtmpfs 892844 0 892844 0% /dev
tmpfs 921932 0 921932 0% /dev/shm
tmpfs 921932 9928 912004 2% /run
tmpfs 921932 0 921932 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17811456 4496616 13314840 26% /
/dev/nvme0n1p1 1038336 234200 804136 23% /boot
tmpfs 184384 1168 183216 1% /run/user/42
tmpfs 184384 4644 179740 3% /run/user/0
/dev/sr0 8238560 8238560 0 100% /run/media/root/RHEL-8-2-0-BaseOS-x86_64
[root@localhost ~]#
[root@localhost ~]# mount /dev/sr0 /mnt/cdrom/
mount: /mnt/cdrom: WARNING: device write-protected, mounted read-only.
[root@localhost ~]#
[root@localhost ~]# rpm -qa |grep postgresql
[root@localhost ~]#
[root@localhost ~]# dnf install postgresql -y
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
BaseOS 2.7 MB/s | 2.8 kB 00:00
AppStream 3.1 MB/s | 3.2 kB 00:00
依賴關(guān)系解決。
================================================================================
軟件包 架構(gòu) 版本 倉庫 大小
================================================================================
安裝:
postgresql x86_64 10.6-1.module+el8+2469+5ecd5aae AppStream 1.5 M
安裝依賴關(guān)系:
libpq x86_64 12.1-3.el8 AppStream 195 k
啟用模塊流:
postgresql 10
事務(wù)概要
================================================================================
安裝 2 軟件包
總計(jì):1.7 M
安裝大?。?.4 M
下載軟件包:
運(yùn)行事務(wù)檢查
事務(wù)檢查成功。
運(yùn)行事務(wù)測試
事務(wù)測試成功。
運(yùn)行事務(wù)
準(zhǔn)備中 : 1/1
安裝 : libpq-12.1-3.el8.x86_64 1/2
安裝 : postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64 2/2
運(yùn)行腳本: postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64 2/2
驗(yàn)證 : libpq-12.1-3.el8.x86_64 1/2
驗(yàn)證 : postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64 2/2
Installed products updated.
已安裝:
libpq-12.1-3.el8.x86_64 postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64
完畢!
[root@localhost ~]#
在客戶機(jī)上登錄
psql -h 192.168.10.1 -U postgres
[root@localhost ~]# psql -h 192.168.10.1 -U postgres
用戶 postgres 的口令:
psql (10.6)
輸入 "help" 來獲取幫助信息.
postgres=# \l
數(shù)據(jù)庫列表
名稱 | 擁有者 | 字元編碼 | 校對規(guī)則 | Ctype | 存取權(quán)限
-----------+----------+----------+-------------+-------------+------------------
-----
db1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
db2 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/post
gres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/post
gres
(5 行記錄)
postgres=#
進(jìn)入db1 再查看employee的表
postgres=# \c db
致命錯誤: 數(shù)據(jù)庫 "db" 不存在
保留上一次連接
postgres=# \c db1
您現(xiàn)在已經(jīng)連接到數(shù)據(jù)庫 "db1",用戶 "postgres".
db1=#
db1=# select * from employee;
no | name | gender
----------------------+--------------------------------+--------
0002 | Tom | M
0001 | Alice | F
(2 行記錄)
db1=#