MySQL關係型數據庫目前算是互聯網公司使用最多的。前兩天看到一個推文,相對比國內使用MySQL,PostgreSQL在國內的普及貌似不高?國外像網絡電話公司Skype公司等在大量使用PostgreSQL
作爲互聯網從業者,保持學習是必須的。開始學習PostgreSQL作爲技術儲備
PostgreSQL 二進制安裝
Centos下二進制安裝一般是藉助於 YUM 來安裝。在 PostgreSQL的官網選定版本和操作系統之後,會自動生成YUM安裝腳本
對應的頁面地址爲 https://www.postgresql.org/download/linux/redhat/
選擇對應的 PostgreSQL版本、操作系統和架構,下面就生成了 YUM安裝命令
安裝命令
# 先安裝PostgreSQL的YUM源
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# 安裝server
sudo yum install -y postgresql14-server
# 先必須進行初始化
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
# 然後可以選擇性的開啓 開機自啓動
sudo systemctl enable postgresql-14
# 啓動服務
sudo systemctl start postgresql-14
但是有個問題
PostgreSQL 初始化數據庫之後,默認的數據目錄是在
/var/lib/pgsql
,但是該目錄是在根分區
下,一般建議放到獨立的數據目錄下
所以這裏進行如下操作
# 新增 數據磁盤目錄
mkdir /data/databases
# 停止數據庫
systemctl stop postgresql-14
# 移動原始數據目錄
mv /var/lib/pgsql /data/databases/
# 創建軟連
cd /var/lib && ln -s /data/databases/pgsql pgsql
# 然後啓動服務
systemctl start postgresql-14
最後驗證安裝是否成功
# 切換到 postgres 用戶(yum安裝時自動生成)
[root@test-demo-01-vm]$ su - postgres
# 直接輸入 psql 回車, 輸出 psql (14.6) 就代表安裝成功
[postgres@test-demo-01-vm]$ psql
psql (14.6)
Type "help" for help.
# \l 列舉目前實例中的所有數據庫,類似mysql中的 show databases ;
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres=# \dg
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
PostgreSQL 說明
1、從 \l
列舉出來的數據庫清單中,看到有三個數據庫
-
PostgreSQL中默認有兩個特殊的數據庫,template0/template1 模板數據庫
就是說可以在這兩個數據庫中新增一些表、視圖等,然後創建數據庫的時候可以指定從那個模板數據庫來
克隆
,這樣新增的數據庫就會有對應的模板數據庫包括的表或者視圖等創建數據庫不指定模板數據庫的時候,默認從
template1
克隆template0 是默認
最簡化的數據庫
,它不能連接到該庫,也不能對其就行修改,要保持它的"乾淨"
使用 template1 模板庫建庫時不可指定新的 encoding 和 locale,而 template0 可以
默認這兩個模板數據庫都不可以被刪除
-
postgres 庫是默認創建的非模板數據庫,它是屬於 postgres 數據庫用戶, 在Centos服務器還有個 postgres Linux用戶
1、所以這裏注意這三個
postgres
的區別2、Linux下切換到postgres用戶直接使用
客戶端工具psql
連接時不加參數登錄,其實就是使用 postgres 數據庫用戶登錄3、
postgres
數據庫用戶是默認的超級用戶
2、關於 PostgreSQL中的用戶和角色
在PostgreSQL 中使用角色
來管理權限,可以把一系列權限分配給角色,當然也可以把權限分配給用戶。
所以從這個角度理解的話,Postgresql 用戶和角色是一回事
,用戶也是角色。 從上面的\du
或者 \dg
的結果就能知道
📢 PostgreSQL中的所有命令都是
\
開頭
PostgreSQL 基本命令
1、數據庫操作
列舉數據庫\l
、連接數據庫 \c dbname
template1=# create database colinspace ;
CREATE DATABASE
template1=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+----------+----------+-------------+-------------+-----------------------
colinspace | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
template1=# \c colinspace
You are now connected to database "colinspace" as user "postgres".
2、表操作
- 列舉數據庫下的表
\d
- 查詢具體表結構信息
\d tablename
- 查詢表的索引信息
\d table_index_name
colinspace=# \d
Did not find any relations.
colinspace=# create table temp_app(id int not null primary key, name varchar(32) not null) ;
CREATE TABLE
colinspace=# \d
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | temp_app | table | postgres
(1 row)
colinspace=# \d temp_app
Table "public.temp_app"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
id | integer | | not null |
name | character varying(32) | | not null |
Indexes:
"temp_app_pkey" PRIMARY KEY, btree (id)
colinspace=# \d temp_app_pkey
Index "public.temp_app_pkey"
Column | Type | Key? | Definition
--------+---------+------+------------
id | integer | yes | id
primary key, btree, for table "public.temp_app"
此外
- 支持通配符
*
和?
,查詢滿足規則的表或者索引等 -
\d+
詳細更加詳細的表信息 -
\dt
只顯示匹配的表 -
\di
只顯示索引 -
\ds
只顯示序列 -
\dv
只顯示視圖 -
\df
只顯示函數 -
\dn
列出所有的 schema 模式 -
\db
列出所有的表空間 -
\du
或者\dg
列出數據庫中的所有角色後者用戶 -
\dp
或者\z
顯示錶的權限分配情況
3、特殊命令
3.1、\timing on/off
顯示和關閉 SQL 已執行的時間
colinspace=# \timing on
Timing is on.
colinspace=# select * from temp_app ;
id | name
----+------
(0 rows)
Time: 0.729 ms
colinspace=# \timing off
Timing is off.
3.2、\encoding utf8/gbk
等設置客戶端的字符編碼
3.3、\pset border 0/1/2
設置輸出的格式
- 0 表示輸出的內容無邊框
- 1 表示輸出的邊框只在內部 (默認行爲)
- 2 表示內外都有邊框
colinspace=# \pset border 0
Border style is 0.
colinspace=# \d
List of relations
Schema Name Type Owner
------ -------- ----- --------
public temp_app table postgres
(1 row)
colinspace=# \pset border 1
Border style is 1.
colinspace=# \d
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | temp_app | table | postgres
(1 row)
colinspace=# \pset border 2
Border style is 2.
colinspace=# \d
List of relations
+--------+----------+-------+----------+
| Schema | Name | Type | Owner |
+--------+----------+-------+----------+
| public | temp_app | table | postgres |
+--------+----------+-------+----------+
(1 row)
3.4、\x
類似MySQL的在命令之後添加\G
3.5、\i filename
執行存儲在外部文件中的sql文件或者命令, 參數是隻要文件名,不帶後綴
當然想要查看更多的命令及其用法,可以使用
\?
如果有在學PostgreSQL的,可以一起交流學習~