【數據庫】PostgreSQL數據庫入門(一)

PostgreSQL是一款開源的關係型數據庫,其具有豐富的數據類型和強大的SQL編程能力,同時還支持存儲非關係型數據。

1. 安裝與配置

本次安裝環境是centos7,PostgreSQL數據庫版本是12。Debian系Linux發行版可以使用APT直接安裝。

  1. 安裝倉庫源:yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  2. 安裝PostgreSQL客戶端、服務端程序:yum install -y postgresql12 postgresql12-server
  3. 初始化數據庫:/usr/pgsql-12/bin/postgresql-12-setup initdb
  4. 更改配置,以保證客戶端正確連接PostgreSQL服務器。
# 在/var/lib/pgsql/12/data/postgresql.conf中追加以下配置,保證服務端可以監聽任意主機。
listen_addresses = '*'
# 在/var/lib/pgsql/12/data/pg_hba.conf中追加以下配置,允許任意ip遠程客戶端進行訪問,訪問驗證方式爲密碼驗證
host  all     all           0.0.0.0/0            password
  1. 修改postgres用戶密碼,或創建新用戶用於連接數據庫。在安裝PostgreSQL數據庫過程中,安裝程序會自動創建postgres用戶作爲默認數據庫管理員,如果想使用postgres用戶進行登錄,需要對其密碼進行修改,操作步驟如下:
# 1.在root用戶shell下執行su - postgres切換到postgres用戶shell
[root@localhost ~]# su - postgres
Last login: Mon Jun 22 16:30:45 CST 2020 on pts/0
-bash-4.2$ 

# 2. 執行psql進入PostgreSQL CLI
-bash-4.2$ psql
psql (12.3)
Type "help" for help.
postgres=# 

# 3. 完成密碼更改(需要進行密碼確認)
postgres=# \password postgres
Enter new password: 
Enter it again:
  1. PostgreSQL默認使用的是5432端口,需對5432 端口或者postgresql服務進行放通。以下兩條命令執行任意一條即可
firewall-cmd --add-service=postgresql
firewall-cmd --add-port=5432
  1. 使用navicat進行數據庫連接。只需填入連接名、主機、密碼信息即可。
    在這裏插入圖片描述

2. PostgreSQL整體架構

在這裏插入圖片描述
Postgres數據庫依靠多個線程進程來維持整個數據庫生態。
postmaster進程是PostgreSQL的主控,用於開啓或者關閉數據庫實例。
logger進程記錄數據庫系統產生的日誌。
checkpointer進程負責記錄檢查點,方便進行數據庫冗災和恢復。
background writer進程負責將客戶端執行的數據庫操作產生的數據改動寫入文件中。
wal writer進程負責記錄WAL(Write Ahead Log)日誌,記錄數據庫修改操作,這樣就使得不用實時將數據寫入數據庫,從而減輕數據庫壓力。
autovacuum launcher負責清理delete後的數據。
stats collector負責統計數據收集。

3. PostgreSQL常用數據類型

類型 名稱 描述
數值類型 smallint、integer、bigint、decimal、numeric、real、double 用於描述數值
自增數值類型 serial、bigserial 常用於主鍵自增,bigserial範圍更大
金錢數值類型 money 解決浮點數不精確問題,直接表示金錢數量
布爾類型 boolean 取值範圍爲true,false
時間類型 time、date、timestamp、interval timestamp可以表示完整時間
字符類型 char、varchar、text 用於描述字符串,字符串需要轉義可以使用E’text\ttext2’格式,可以直接使用單引號對單引號進行轉義
網絡類型 inet、cidr、macaddr 分別用於描述IP、無類別域間路由及物理mac地址
圖形類型 point、line、lseg、box、path、polygon、circle 圖形參數描述
JSON類型 json、jsonb 分別用於描述json和二進制json數據
Bytea類型 bytea 二進制數據,可以存放二進制資源文件

4.使用示例

  1. 創建數據表。
create table tb_user (                    
	id serial primary key,
	name varchar(32),
	gender boolean comment 'true:male,false:female',
	balance money,
	cr_time timestamp,
	ip_addr cidr,
	mac_addr macaddr);
  1. 使用\d tb_user查看數據表結構。
postgres=# \d tb_user;
                                       Table "public.tb_user"
  Column  |            Type             | Collation | Nullable |               Default               
----------+-----------------------------+-----------+----------+-------------------------------------
 id       | integer                     |           | not null | nextval('tb_user_id_seq'::regclass)
 name     | character varying(32)       |           |          | 
 gender   | boolean                     |           |          | 
 balance  | money                       |           |          | 
 cr_time  | timestamp without time zone |           |          | 
 ip_addr  | inet                        |           |          | 
 mac_addr | macaddr                     |           |          | 
Indexes:
    "tb_user_pkey" PRIMARY KEY, btree (id)
  1. 插入數據。
insert into tb_user(name, gender, balance, cr_time, ip_addr, mac_addr) values('zhangsan', false, 7731, now(), '192.168.1.10', '2A:3B:4C:5E:6E:7F');
  1. 由於數據較長,我們可以使用\x命令打開擴展視圖,並嘗試執行查詢。
postgres=# \x
Expanded display is on.
postgres=# select * from tb_user where cr_time>now() - interval '10h';
-[ RECORD 1 ]------------------------
id       | 1
name     | huangwei
gender   | t
balance  | $3,604.00
cr_time  | 2020-06-22 00:22:52.376134
ip_addr  | 192.168.1.1
mac_addr | 2a:3b:4c:5d:6e:7f
-[ RECORD 2 ]------------------------
id       | 2
name     | zhangsan
gender   | f
balance  | $7,731.00
cr_time  | 2020-06-22 01:14:25.743003
ip_addr  | 192.168.1.10
mac_addr | 2a:3b:4c:5e:6e:7f
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章