1. 總結(jié)pg和mysql的優(yōu)劣勢。
1.1 PostgreSQL
- 優(yōu)點(diǎn):
強(qiáng)大的對象關(guān)系映射功能
豐富的數(shù)據(jù)類型
強(qiáng)大的事務(wù)處理能力
豐富的統(tǒng)計(jì)函數(shù)和操作符
支持復(fù)雜的SQL查詢
支持用戶定義的數(shù)據(jù)類型和函數(shù)
支持NoSQL特性,如JSONB數(shù)據(jù)類型
支持地理空間數(shù)據(jù)處理
支持多版本并發(fā)控制 (MVCC)
支持非阻塞查詢
支持函數(shù)索引
支持表分區(qū)
支持可擴(kuò)展性,通過Postgres-XL或者Pgpool-II等第三方集群解決方案
- 缺點(diǎn):
學(xué)習(xí)曲線較為陡峭
對于簡單的操作,如插入或選擇,可能表現(xiàn)不如MySQL
在大數(shù)據(jù)集上進(jìn)行復(fù)雜的操作時(shí),性能可能不如MySQL
1.2 MySQL
- 優(yōu)點(diǎn):
簡單易學(xué)
速度快,尤其是在讀操作為主的場景下
提供了大量的默認(rèn)值和內(nèi)置函數(shù),使得開發(fā)者不需要自行編寫復(fù)雜的SQL查詢
支持MySQL Cluster,能夠提供高可用性和強(qiáng)一致性保證
支持InnoDB存儲(chǔ)引擎,提供了行級(jí)鎖定和外鍵約束
支持MySQL Workbench,一個(gè)可視化的數(shù)據(jù)庫設(shè)計(jì)、管理和優(yōu)化工具
有MySQL Community Server、MySQL Enterprise Edition等不同版本供選擇
- 缺點(diǎn):
功能不如PostgreSQL豐富
不支持NoSQL特性
不支持地理空間數(shù)據(jù)處理
不支持MVCC
不支持表分區(qū)
不支持函數(shù)索引
可擴(kuò)展性不如PostgreSQL
1.3 總結(jié):
選擇哪種數(shù)據(jù)庫取決于你的具體需求。如果你需要強(qiáng)大的對象關(guān)系映射和復(fù)雜查詢處理,PostgreSQL可能更適合。如果你需要快速的讀操作和簡單的數(shù)據(jù)管理,MySQL可能是更好的選擇。
2. 總結(jié)pg二進(jìn)制安裝和編譯安裝。
#二進(jìn)制安裝
apt install curl ca-certificates
install -d /usr/share/postgresql-common/pgdg
curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
apt update && apt -y install postgresql
#范例:ubuntu上源碼編譯安裝postgresql
#安裝必要包
root@ubuntu:~# apt update && apt install -y gcc make libreadline-dev zlib1g-dev
#解壓縮
root@ubuntu:~# tar xf postgresql-14.2.tar.gz
root@ubuntu:~# cd postgresql-14.2/
#編譯安裝
root@ubuntu:~/postgresql-14.2# ./configure --prefix=/apps/pgsql
#加world有man幫助
root@ubuntu:~/postgresql-14.2# make -j 4 world
root@ubuntu:~/postgresql-14.2# make install-world
查看路徑
root@ubuntu:~/postgresql-14.2# tree /apps/pgsql/
創(chuàng)建數(shù)據(jù)庫用戶和組
root@ubuntu:~/postgresql-14.2# useradd -s /bin/bash -m -d /home/postgres postgres
修改postgres密碼
root@ubuntu:~/postgresql-14.2# echo -e '123456\n123456' | passwd postgres
#或者echo postgres:123456 | chpasswd
#創(chuàng)建數(shù)據(jù)目錄并授權(quán)
root@ubuntu:~# mkdir -pv /pgsql/data
root@ubuntu:~# chown postgres.postgres /pgsql/data/
#設(shè)置環(huán)境變量
root@ubuntu:~# vim /etc/profile.d/pgsql.sh
#!/bin/bash
export PGHOME=/apps/pgsql
export PATH=$PGHOME/bin/:$PATH
export PGDATA=/pgsql/data
export PGUSER=postgres
export MANPATH=/apps/pgsql/share/man:$MANPATH
root@ubuntu:/usr/local/src/postgresql-14.2# su - postgres
#初始化并按照預(yù)先定義變量存放路徑
postgres@ubuntu:~$ initdb
postgres@ubuntu:~$ ls /pgsql/data
#啟動(dòng)postgresql數(shù)據(jù)庫服務(wù)
postgres@ubuntu:~$ pg_ctl -l logfile start
#查看端口號(hào)5432確認(rèn)服務(wù)開啟
postgres@ubuntu:~$ ss -ntl
postgres@ubuntu:~$ psql
設(shè)置開機(jī)自啟動(dòng)
第一種方法:
root@ubuntu:~# vim /etc/rc.local
root@ubuntu:~#cd /usr/local/src/postgresql-14.2/
root@ubuntu:/usr/local/src/postgresql-14.2# cp contrib/start-scripts/linux /etc/init.d/postgresql
root@ubuntu:/usr/local/src/postgresql-14.2# vim /etc/init.d/postgresql
prefix=/apps/pgsql
PGDATA="/pgsql/data"
root@ubuntu:~# chmod +x /etc/init.d/postgresql
root@ubuntu:~# /etc/init.d/postgresql start
Starting PostgreSQL: ok
root@ubuntu:~# chmod +x /etc/rc.local
第二種方法:
root@ubuntu:~# vim /etc/rc.local
su - postgres -c "pg_ctl -l logfile start"
下面命令也可以啟動(dòng)服務(wù)
root@ubuntu:~# service postgresql start
第三種方法:
創(chuàng)建service文件
systemctl cat postgresql.service
#/lib/systemd/system/postgresql.service
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
User=postgres
Group=postgres
ExecStart=/apps/pgsql/bin/postmaster -D /pgsql/data
ExecReload=/bin/kill -HUP
[Install]
WantedBy=multi-user.target
3. 總結(jié)pg服務(wù)管理相關(guān)命令 pg_ctl 和pgsql命令選項(xiàng)及示例和不同系統(tǒng)的初始化操作
- pg_ctl命令選項(xiàng)及示例
#初始化
pg_ctl init[db] [-D DATADIR] [-s] [-o OPTIONS]
#啟動(dòng)服務(wù)
pg_ctl start [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]
[-o OPTIONS] [-p PATH] [-c]
#停止服務(wù)
pg_ctl stop [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
#重啟服務(wù)
pg_ctl restart [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
[-o OPTIONS] [-c]
#重新加載
pg_ctl reload [-D DATADIR] [-s]
#查看狀態(tài)
pg_ctl status [-D DATADIR]
#從節(jié)點(diǎn)提升為主節(jié)點(diǎn)
pg_ctl promote [-D DATADIR] [-W] [-t SECS] [-s]
- pgsql命令選項(xiàng)及示例
psql [-h host] [-p 端口] 數(shù)據(jù)庫名 -U [用戶名]
范例:
#創(chuàng)建管理員賬號(hào)
hellodb=# CREATE ROLE admin WITH SUPERUSER LOGIN PASSWORD '123456';
[postgres@rocky8 ~]$ psql -h 10.0.0.200 -U admin -d db1 -p 5432
- 不同系統(tǒng)的初始化操作
#初始化方法一
postgres@ubuntu:~$ pg_ctl init -D /pgsql/data2
#初始化方法二
postgres@ubuntu:~$ initdb
4. 總結(jié)pg數(shù)據(jù)庫結(jié)構(gòu)組織
在一個(gè)postgresql數(shù)據(jù)庫系統(tǒng)中,數(shù)據(jù)的組織結(jié)構(gòu)可以分為以下五層:
實(shí)例:一個(gè)postgresql對應(yīng)一個(gè)安裝的數(shù)據(jù)目錄$PGDATA,即一個(gè)instance實(shí)例
數(shù)據(jù)庫:一個(gè)postgresql數(shù)據(jù)庫服務(wù)下可以管理多個(gè)數(shù)據(jù)庫,當(dāng)應(yīng)用連接到一個(gè)數(shù)據(jù)庫時(shí),一般只能訪問這 個(gè)數(shù)據(jù)庫中的數(shù)據(jù),而不能訪問其他數(shù)據(jù)庫中的內(nèi)容,默認(rèn)情況下初始實(shí)例只有三個(gè)數(shù)據(jù)庫:postgres、 template0、template1
模式:一個(gè)數(shù)據(jù)庫可以創(chuàng)建多個(gè)不同的名稱空間即Schema,用于分割不同的業(yè)務(wù)數(shù)據(jù)
表和索引:一個(gè)數(shù)據(jù)庫可以有多個(gè)表和索引。在postgresql中表的術(shù)語稱為relation,而在其他數(shù)據(jù)庫中通常叫Table
行和列:每張表中有很多列和行的數(shù)據(jù)。在postgresql中行的術(shù)語一般為Tuple,而在其他數(shù)據(jù)庫中則叫Row
5. 實(shí)現(xiàn)pg遠(yuǎn)程連接。輸入密碼和無密碼登陸
遠(yuǎn)程連接配置:
- [1] 更改遠(yuǎn)程連接地址(可以是*或者0.0.0.0或者網(wǎng)段)
postgres@ubuntu:~$ echo $PGDATA
/pgsql/data
postgres@ubuntu:~$ vim $PGDATA/postgresql.conf
listen_addresses = '0.0.0.0'
- [2]遠(yuǎn)程連接賬號(hào)密碼相關(guān)設(shè)置
postgres@ubuntu:~$ vim $PGDATA/pg_hba.conf
#添加此行
host all all 0.0.0.0/0 md5
postgres@ubuntu:~$ pg_ctl restart
#修改遠(yuǎn)程賬號(hào)的密碼
postgres=# ALTER USER postgres with password '123456';
#測試遠(yuǎn)程登錄(輸入密碼)
[postgres@rocky8 ~]$ psql -d postgres -h 10.0.0.200 -p 5432 -U postgres
Password for user postgres:
#測試遠(yuǎn)程登錄(無密碼)
若寫腳本,非交互式:
[postgres@rocky8 ~]$ cat > .pgpass <<EOF
> 10.0.0.200:5432:testdb:postgres:123456
> EOF
chmod 600 .pgpass
#再連接不需要輸入密碼了
[postgres@rocky8 ~]$ psql -h10.0.0.200 db1 postgres
6. 總結(jié)庫,模式,表的添加和刪除操作。表數(shù)據(jù)的CURD。同時(shí)總結(jié)相關(guān)信息查看語句。
- 數(shù)據(jù)庫添加
[root@rocky8 ~]# createdb -h 10.0.0.200 -U postgres db2 創(chuàng)建數(shù)據(jù)庫 - 數(shù)據(jù)庫刪除
postgres=# drop database db2; - 模式添加
postgres=# create schema test; - 模式刪除
postgres=# drop schema test; - 表的添加
hellodb=# CREATE TABLE t1(id int); - 表的刪除
hellodb=# drop table t1; - 表數(shù)據(jù)的創(chuàng)建
hellodb=# insert into teachers values(6,'Hu Xia',23,'F'); - 表數(shù)據(jù)的更新
hellodb=# update teachers set name='Xiao Man' where tid=5; - 表數(shù)據(jù)的讀取
hellodb=# select * from teachers; - 表數(shù)據(jù)的刪除
hellodb=# delete from teachers where tid=5; - 相關(guān)信息查看語句
#查詢數(shù)據(jù)庫明細(xì)
hellodb=# \l+
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Descrip
tion
-----------+----------+----------+---------+---------+-----------------------+---------+------------+-----------------------
---------------------
hellodb | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/postgres +| 8441 kB | pg_default |
| | | | | postgres=CTc/postgres | | |
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | 8233 kB | pg_default | default administrative
connection database
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +| 8081 kB | pg_default | unmodifiable empty dat
abase
| | | | | postgres=CTc/postgres | | |
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +| 8081 kB | pg_default | default template for n
ew databases
| | | | | postgres=CTc/postgres | | |
(4 rows)
#查看用戶
hellodb=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
repluser | Replication | {}
#查看當(dāng)前數(shù)據(jù)庫所有表
hellodb=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | classes | table | postgres
public | coc | table | postgres
public | courses | table | postgres
public | scores | table | postgres
public | students | table | postgres
public | teachers | table | postgres
public | toc | table | postgres
(7 rows)
#查看版本信息
hellodb=# select version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 14.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-22), 64-bit
(1 row)
#查看系統(tǒng)啟動(dòng)時(shí)間
testdb=# select pg_postmaster_start_time();
pg_postmaster_start_time
-------------------------------
2024-08-07 05:52:30.144636+08
(1 row)
Time: 0.800 ms
#加載服務(wù)的時(shí)間
testdb=# select pg_conf_load_time();
pg_conf_load_time
------------------------------
2024-08-07 05:52:30.03899+08
(1 row)
#查看時(shí)區(qū)
testdb=# show timezone;
TimeZone
---------------
Asia/Shanghai
#更改時(shí)區(qū)(進(jìn)配置文件搜索timezone)
postgres@ubuntu:~$ vim /pgsql/data/postgresql.conf
timezone = 'Asia/Shanghai'
log_timezone = 'Asia/Shanghai'
postgres@ubuntu:~$ pg_ctl reload
#查看當(dāng)前時(shí)間
testdb=# select now();
now
-------------------------------
2024-08-07 07:28:30.964245+08
#查看當(dāng)前用戶
testdb=# select user;
user
----------
postgres
#遠(yuǎn)程連接,查詢本機(jī)IP和端口
postgres=# select inet_client_addr(),inet_client_port();
inet_client_addr | inet_client_port
------------------+------------------
10.0.0.8 | 43870
#遠(yuǎn)程連接,查詢服務(wù)器IP和端口
postgres=# select inet_server_addr(),inet_server_port();
inet_server_addr | inet_server_port
------------------+------------------
10.0.0.200 | 5432
#查看進(jìn)程id
postgres=# select pg_backend_pid();
pg_backend_pid
----------------
38313
#查看系統(tǒng)內(nèi)置變量
postgres=# \set
#查看最大連接數(shù)
postgres=# show max_connections;
max_connections
-----------------
100
改最大連接數(shù)需要改配置文件重啟
postgres@ubuntu:~$ vim /pgsql/data/postgresql.conf
max_connections = 1000
postgres@ubuntu:~$ pg_ctl restart
db1=# show max_connections;
max_connections
-----------------
1000
#查看系統(tǒng)函數(shù)
db1=# \dfS
#查看archive默認(rèn)開關(guān)
db1=# show archive_mode;
archive_mode
--------------
off
#查看所有默認(rèn)配置
db1=# show all;
#查看默認(rèn)執(zhí)行維護(hù)任務(wù)時(shí)的內(nèi)存
db1=# show maintenance_work_mem ;
maintenance_work_mem
----------------------
64MB
7. 總結(jié)pg的用戶和角色管理。
#列出所有賬號(hào)
hellodb=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
#權(quán)限
hellodb=# \z
#創(chuàng)建賬號(hào)
hellodb=# create user wang with password '123'; 可以登錄
hellodb=# create role user2 with password '123'; 不能登錄,想要登錄,加with login
#創(chuàng)建管理員賬號(hào)
hellodb=# CREATE ROLE admin WITH SUPERUSER LOGIN PASSWORD '123456';
#修改密碼
hellodb=# ALTER USER admin with password '654321';
#修改權(quán)限
hellodb=# alter user user2 with login;
#刪除賬號(hào)
drop user user2
#更改創(chuàng)建數(shù)據(jù)庫權(quán)限
postgres=# alter user wang with CREATEDB;
#database權(quán)限設(shè)置
GRANT create ON DATEBASE testdb TO wang;
#schema權(quán)限
ALTER SCHEMA wang OWNER to wang;
GRANT select,insert,update,delete ON ALL TABLES IN SCHEMA wang TO wang;
#創(chuàng)建test的schema指定所有者為joe
CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
#object權(quán)限
GRANT select,insert,update,delete ON testdb.t1 TO wang;
#創(chuàng)建數(shù)據(jù)庫并指定所有者的用戶
create user wang with password '123456';
CREATE DATABASE testdb OWNER wang;
范例:創(chuàng)建業(yè)務(wù)用戶和授權(quán)
postgres=# create database pinxixi;
postgres=# \c pinxixi
pinxixi=# create user wanrentuan with password '123456';
#方法一
pinxixi=# create schema wanrentuan;
pinxixi=# ALTER SCHEMA wanrentuan OWNER to wanrentuan;
#方法二
pinxixi=# CREATE SCHEMA AUTHORIZATION wanrentuan;
#方法三
pinxixi=# GRANT select,insert,update,delete ON ALL TABLES IN SCHEMA wanrentuan to wanrentuan;
范例:
#創(chuàng)建一個(gè)名為“readonly”的用戶
CREATE USER readonly with password '123456';
#把在public的schema下現(xiàn)有的所有表的SELECT 權(quán)限給用戶readonly
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;
8. 添加mage用戶,magedu模式,準(zhǔn)備zabbix庫,配置mage用戶的默認(rèn)模式magedu,要求mage用戶給zabbix庫有所有權(quán)限。
#創(chuàng)建mage賬號(hào)
[root@rocky8 ~]# su - postgres
[postgres@rocky8 ~]$ psql
postgres=# create user mage with password '123456';
#magedu模式
postgres=# create schema magedu
#準(zhǔn)備zabbix庫
postgres=# create database zabbix;
#配置mage用戶的默認(rèn)模式magedu
zabbix=# create schema magedu authorization mage;
#mage用戶給zabbix庫所有權(quán)限
zabbix=# alter database zabbix owner to mage;
9. 總結(jié)pgsql的進(jìn)程結(jié)構(gòu),說明進(jìn)程間如何協(xié)同工作的。

- Postmaster主進(jìn)程
它是整個(gè)數(shù)據(jù)庫實(shí)例的主控制進(jìn)程,負(fù)責(zé)啟動(dòng)和關(guān)閉該數(shù)據(jù)庫實(shí)例 - BgWriter 后臺(tái)寫進(jìn)程
WalWriter 預(yù)寫式日志進(jìn)程 寫日志再寫入數(shù)據(jù)優(yōu)點(diǎn):既安全又快 存放路徑:wal - Checkpointer 檢查點(diǎn)進(jìn)程
所有臟數(shù)據(jù)寫入磁盤 - PgArch 歸檔進(jìn)程
默認(rèn)沒有此進(jìn)程,生產(chǎn)中必須開啟此功能,備份用 - AutoVacuum 自動(dòng)清理進(jìn)程
執(zhí)行delete操作時(shí),舊的數(shù)據(jù)并不會(huì)立即被刪除,在更新數(shù)據(jù)時(shí),也不會(huì)在舊的數(shù)據(jù)上做更新,而是新生成一行數(shù)據(jù)。舊的數(shù)據(jù)只是被標(biāo)識(shí)為刪除狀態(tài),在沒有并發(fā)的其他事務(wù)讀到這些日數(shù)據(jù)時(shí),它們才會(huì)被清除掉 。
autovacuum lanucher 負(fù)責(zé)回收垃圾數(shù)據(jù)的master進(jìn)程如果開啟了autovacuum的話,那么.postmaster會(huì)fork這個(gè)講程 。autovacuum worker 負(fù)責(zé)回收垃圾數(shù)據(jù)的worker進(jìn)程,是lanucher進(jìn)程fork出來的 - PgStat 統(tǒng)計(jì)數(shù)據(jù)收集進(jìn)程
此進(jìn)程主要做數(shù)據(jù)的統(tǒng)計(jì)收集工作 。收集的信息主要用于查詢優(yōu)化時(shí)的代價(jià)估算。統(tǒng)計(jì)的數(shù)據(jù)包括對一個(gè)表或索引進(jìn)行的插入、刪.除、更新操作,磁盤塊讀寫的次數(shù)以及行的讀次數(shù)等。系統(tǒng)表pg statistic中存儲(chǔ)了PgStat收集的各類統(tǒng)計(jì)信息 - SysLogger 系統(tǒng)日志進(jìn)程
默認(rèn)沒有此進(jìn)程,配置文件 postgresgl.conf 設(shè)置參數(shù)logging collect設(shè)置為"on"時(shí), 主進(jìn)程才會(huì)啟動(dòng)SysLogger輔助進(jìn)程。它從Postmaster主進(jìn)程、所有的服務(wù)進(jìn)程以及其他輔助進(jìn)程收集所有的stderr輸出,并將這0些輸出寫入到日志文件中 - startup 啟動(dòng)進(jìn)程
用于數(shù)據(jù)集恢復(fù)的進(jìn)程 - Session 會(huì)話進(jìn)程
每一個(gè)用戶發(fā)起連接后,一旦驗(yàn)證成功,postmaster進(jìn)程就會(huì)fork一個(gè)新的子進(jìn)程負(fù)責(zé)連接此用戶。
通常表現(xiàn)為進(jìn)程形式: postgres postgres [local] idle0
10. 總結(jié)pgsql的數(shù)據(jù)目錄中結(jié)構(gòu),說明每個(gè)文件的作用,并可以配上一些示例說明文件的作用。
#pg_ident.conf用戶映射配置文件
postgres@ubuntu:~$ ls /pgsql/data/pg_ident.conf
#pg_hba.conf賬號(hào)密碼設(shè)置
postgres@ubuntu:~$ ls /pgsql/data/pg_hba.conf
#數(shù)據(jù)文件
每個(gè)索引和表都是一個(gè)單獨(dú)的文件,稱為Segment,默認(rèn)大于1G的Segment會(huì)被分割pg_class.efilenode.1這樣的文件。大小可以在initdb時(shí)通過選項(xiàng)--with-segsize=SEGSIZE指定。
#控制文件
postgres@ubuntu:~$ ls /pgsql/data/global/pg_control
#查看控制文件
postgres@ubuntu:~$ pg_controldata
postgres@ubuntu:~$ ll /pgsql/data/pg_wal