1. 程式人生 > 實用技巧 >PostgreSQL簡介以及簡單使用

PostgreSQL簡介以及簡單使用

1.簡介

PostgreSQL 是一個免費的物件-關係資料庫伺服器(ORDBMS),在靈活的BSD許可證下發行。

PostgreSQL 開發者把它唸作 post-gress-Q-L。

PostgreSQL 的 Slogan 是 "世界上最先進的開源關係型資料庫"。

關於與mysql的區別參考:https://www.cnblogs.com/geekmao/p/8541817.html

2. 安裝

1. 下載安裝包:這裡

2. 安裝

直接下一步即可,需要注意:

(1) postgresql的預設賬號是postgres

(2)postgresql的預設埠是5432

3.簡單使用

1. 登入

psql -U postgres

2.檢視存在的庫

\l

3.建立資料庫並且進入以及檢視

postgres=# CREATE DATABASE test;
CREATE DATABASE
postgres=# \c test;
您現在已經連線到資料庫 "test",使用者 "postgres".

4.建立表以及檢視資訊

test-# CREATE TABLE DEPARTMENT(
test(#    ID INT PRIMARY KEY      NOT NULL,
test(#    DEPT           CHAR(50) NOT NULL,
test(#    EMP_ID         INT      NOT NULL
test(# );
錯誤:  語法錯誤 在 
"CREATE" 或附近的 第2行CREATE TABLE DEPARTMENT( ^ test=# CREATE TABLE DEPARTMENT( test(# ID INT PRIMARY KEY NOT NULL, test(# DEPT CHAR(50) NOT NULL, test(# EMP_ID INT NOT NULL test(# ); CREATE TABLE test=# \d 關聯列表 架構模式 | 名稱 | 型別 | 擁有者 ----------+------------+--------+---------- public
| department | 資料表 | postgres (1 行記錄) test=# \d department 資料表 "public.department" 欄位 | 型別 | Collation | Nullable | Default --------+---------------+-----------+----------+--------- id | integer | | not null | dept | character(50) | | not null | emp_id | integer | | not null | 索引: "department_pkey" PRIMARY KEY, btree (id)

5. 插入一條資料以及搜尋

test=# INSERT INTO department values(1, '測試', 1);
INSERT 0 1
test=# select * from department;
 id |                         dept                         | emp_id
----+------------------------------------------------------+--------
  1 | 測試                                                 |      1
(1 行記錄)

6. 在插入一條進行分頁查詢

test=# INSERT INTO department values(2, '測試2', 2);
INSERT 0 1
test=# select * from department limit 1 offset 1;
 id |                         dept                         | emp_id
----+------------------------------------------------------+--------
  2 | 測試2                                                |      2
(1 行記錄)

7. explain簡單分析(列出的資訊少於mysql的explain分析)

test=# explain select * from department where id = 1;
                                     QUERY PLAN
------------------------------------------------------------------------------------
 Index Scan using department_pkey on department  (cost=0.15..8.17 rows=1 width=212)
   Index Cond: (id = 1)
(2 行記錄)

  其他事務類似於mysql,鎖也有共享鎖和排它鎖。

  只是公司用到了postgresql,所以簡單的使用下postgresql。