PostgreSQL常用命令(持續更新)

PostgreSQL 10.18 on Windows

--

 

PostgreSQL\10\bin 下可執行exe文件(部分):

psql.exe

createdb.exe

dropdb.exe

initdb.exe

createuser.exe

dropuser.exe

 

本文主要展示 psql的使用,以及進入PostgreSQL控制檯的命令使用。

 

超級用戶(角色):postgres

注,下文如未特別說明,都是使用此用戶進行操作。

 

基本使用

使用psql連接DB服務器

幫助信息:

>psql --help

訪問本機DB服務器:

>psql -U postgres
用戶 postgres 的口令:
psql (10.18)
輸入 "help" 來獲取幫助信息.

postgres=#

 

幫助信息:help

postgres=# help
您正在使用psql, 這是一種用於訪問PostgreSQL的命令行界面
鍵入: \copyright 顯示發行條款
       \h 顯示 SQL 命令的說明
       \? 顯示 pgsql 命令的說明
       \g 或者以分號(;)結尾以執行查詢
       \q 退出
postgres=#

postgres=# \copyright
PostgreSQL Database Management System
(formerly known as Postgres, then as Postgres95)

Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
......省略......

postgres=# \h
可用的說明:
  ABORT                            CREATE USER MAPPING
  ALTER AGGREGATE                  CREATE VIEW
  ALTER COLLATION                  DEALLOCATE
  ALTER CONVERSION                 DECLARE
......省略......

postgres=# \?
一般性
  \copyright            顯示PostgreSQL的使用和發行許可條款
  \crosstabview [COLUMNS] 執行查詢並且以交叉表顯示結果
  \errverbose            以最冗長的形式顯示最近的錯誤消息
  \g [文件] or;     執行查詢 (並把結果寫入文件或 |管道)
......省略......

 

\q 退出腳本。 

 

\h 顯示 SQL腳本\h + 命令,可以查看腳本語法:

postgres=# \h ABORT
命令:       ABORT
描述:       中止目前的事務
語法:
ABORT [ WORK | TRANSACTION ]


postgres=# \h SELECT
命令:       SELECT
描述:       從數據表或視圖中讀取數據
語法:
[ WITH [ RECURSIVE ] with查詢語句(with_query) [, ...] ]
SELECT [ ALL | DISTINCT [ ON ( 表達式 [, ...] ) ] ]
    [ * | 表達式 [ [ AS ] 輸出名稱 ] [, ...] ]
    [ FROM from列表中項 [, ...] ]
    [ WHERE 條件 ]
    [ GROUP BY grouping_element [, ...] ]
    [ HAVING 條件 ]
......省略......

 

\? 分頁顯示 一些命令——非SQL腳本,下面將介紹更多這些命令。

 

說明,
出現“-- More  --”時,
Enter 看下一行,
空格 看下一頁,
q 結束查看。
其中有很多後面要介紹的命令。

 

執行 \? 中查到的一些命令,目前主要用了查看 數據庫的信息。

當前連接信息:

postgres=# \conninfo
以用戶 "postgres" 的身份, 在主機"localhost", 端口"5432"連接到數據庫 "postgres"

注,目前只有一個 postgres 數據庫,後面再新建數據庫後,再講 切換。

查看數據庫列表:\list \list+ \l \l+ (是 L 不是 一)

postgres=# \l
                                                        數據庫列表
   名稱    |  擁有者  | 字元編碼 |            校對規則            |             Ctype              |       存取權限
-----------+----------+----------+--------------------------------+--------------------------------+-----------------------
 postgres  | postgres | UTF8     | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 |
 template0 | postgres | UTF8     | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres          +
           |          |          |                                |                                | postgres=CTc/postgres
 template1 | postgres | UTF8     | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres          +
           |          |          |                                |                                | postgres=CTc/postgres
(3 行記錄)


postgres=# \l+
                                                                                          數據庫列表
   名稱    |  擁有者  | 字元編碼 |            校對規則            |             Ctype              |       存取權限        |  大小   |   表空間   |                    描述                  
-----------+----------+----------+--------------------------------+--------------------------------+-----------------------+---------+------------+--------------------------------------------
 postgres  | postgres | UTF8     | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 |                       | 7575 kB | pg_default | default administrative connection database
 template0 | postgres | UTF8     | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres          +| 7441 kB | pg_default | unmodifiable empty database
           |          |          |                                |                                | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres          +| 7441 kB | pg_default | default template for new databases
           |          |          |                                |                                | postgres=CTc/postgres |         |            |
(3 行記錄)

 

列出表、視圖、序列:

選項說明:S = 顯示系統對象, + = 其餘的詳細信息

postgres=# \d
Did not find any relations.

加了 S ,顯示出了表、視圖等信息
postgres=# \dS
                             關聯列表
  架構模式  |              名稱               |  類型  |  擁有者
------------+---------------------------------+--------+----------
 pg_catalog | pg_aggregate                    | 數據表 | postgres
 pg_catalog | pg_am                           | 數據表 | postgres
 pg_catalog | pg_amop                         | 數據表 | postgres
 pg_catalog | pg_amproc                       | 數據表 | postgres
 pg_catalog | pg_attrdef                      | 數據表 | postgres
 pg_catalog | pg_attribute                    | 數據表 | postgres
 pg_catalog | pg_auth_members                 | 數據表 | postgres
 pg_catalog | pg_authid                       | 數據表 | postgres
 pg_catalog | pg_available_extension_versions | 視圖   | postgres
 pg_catalog | pg_available_extensions         | 視圖   | postgres
 ......省略......
 
 再添加 加號(+),顯示了更多信息,包括表存儲空間
 postgres=# \dS+
                                       關聯列表
  架構模式  |              名稱               |  類型  |  擁有者  |    大小    | 描述
------------+---------------------------------+--------+----------+------------+------
 pg_catalog | pg_aggregate                    | 數據表 | postgres | 48 kB      |
 pg_catalog | pg_am                           | 數據表 | postgres | 40 kB      |
 pg_catalog | pg_amop                         | 數據表 | postgres | 80 kB      |
 pg_catalog | pg_amproc                       | 數據表 | postgres | 64 kB      |
 pg_catalog | pg_attrdef                      | 數據表 | postgres | 8192 bytes |
 pg_catalog | pg_attribute                    | 數據表 | postgres | 416 kB     |
 pg_catalog | pg_auth_members                 | 數據表 | postgres | 40 kB      |
 pg_catalog | pg_authid                       | 數據表 | postgres | 40 kB      |
 pg_catalog | pg_available_extension_versions | 視圖   | postgres | 0 bytes    |
 pg_catalog | pg_available_extensions         | 視圖   | postgres | 0 bytes    |
 ......省略......
 

 

\d[S+]  名稱   :   描述表,視圖,序列,或索引

postgres=# \dS pg_attribute
              數據表 "pg_catalog.pg_attribute"
     欄位      |   類型    | Collation | Nullable | Default
---------------+-----------+-----------+----------+---------
 attrelid      | oid       |           | not null |
 attname       | name      |           | not null |
 atttypid      | oid       |           | not null |
 attstattarget | integer   |           | not null |
 attlen        | smallint  |           | not null |
 attnum        | smallint  |           | not null |
 attndims      | integer   |           | not null |
 attcacheoff   | integer   |           | not null |
 atttypmod     | integer   |           | not null |
 attbyval      | boolean   |           | not null |
 attstorage    | "char"    |           | not null |
 attalign      | "char"    |           | not null |
 attnotnull    | boolean   |           | not null |
 atthasdef     | boolean   |           | not null |
 attidentity   | "char"    |           | not null |
 attisdropped  | boolean   |           | not null |
 attislocal    | boolean   |           | not null |
 attinhcount   | integer   |           | not null |
 attcollation  | oid       |           | not null |
 attacl        | aclitem[] |           |          |
 attoptions    | text[]    |           |          |
 attfdwoptions | text[]    |           |          |
索引:
    "pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname)
    "pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)


postgres=#
postgres=#
postgres=#
postgres=# \dS pg_available_extensions
         視圖 "pg_catalog.pg_available_extensions"
       欄位        | 類型 | Collation | Nullable | Default
-------------------+------+-----------+----------+---------
 name              | name |           |          |
 default_version   | text |           |          |
 installed_version | text |           |          |
 comment           | text |           |          |


postgres=#

 

添加  加號(+) 會顯示更多:

postgres=# \dS+ pg_attribute
                            數據表 "pg_catalog.pg_attribute"
     欄位      |   類型    | Collation | Nullable | Default |   存儲   | 統計目標 | 描述
---------------+-----------+-----------+----------+---------+----------+----------+------
 attrelid      | oid       |           | not null |         | plain    |          |
 attname       | name      |           | not null |         | plain    |          |
 atttypid      | oid       |           | not null |         | plain    |          |
 attstattarget | integer   |           | not null |         | plain    |          |
 attlen        | smallint  |           | not null |         | plain    |          |
 attnum        | smallint  |           | not null |         | plain    |          |
 attndims      | integer   |           | not null |         | plain    |          |
 attcacheoff   | integer   |           | not null |         | plain    |          |
 atttypmod     | integer   |           | not null |         | plain    |          |
 attbyval      | boolean   |           | not null |         | plain    |          |
 attstorage    | "char"    |           | not null |         | plain    |          |
 attalign      | "char"    |           | not null |         | plain    |          |
 attnotnull    | boolean   |           | not null |         | plain    |          |
 atthasdef     | boolean   |           | not null |         | plain    |          |
 attidentity   | "char"    |           | not null |         | plain    |          |
 attisdropped  | boolean   |           | not null |         | plain    |          |
 attislocal    | boolean   |           | not null |         | plain    |          |
 attinhcount   | integer   |           | not null |         | plain    |          |
 attcollation  | oid       |           | not null |         | plain    |          |
 attacl        | aclitem[] |           |          |         | extended |          |
 attoptions    | text[]    |           |          |         | extended |          |
 attfdwoptions | text[]    |           |          |         | extended |          |
索引:
    "pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname)
    "pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)


postgres=#

 

查看PostgreSQL數據庫連接及數量

postgres=# select * from pg_stat_activity;

這個表的信息很多,可以只 查詢其中部分字段:

postgres=# select datid,datname,pid,usename,state,client_addr,query from pg_stat_activity;
 datid | datname  |  pid  | usename  | state  | client_addr |                                      query
-------+----------+-------+----------+--------+-------------+---------------------------------------------------------------------------------
       |          | 18904 |          |        |             |
       |          | 18556 | postgres |        |             |
 12938 | postgres | 16576 | postgres | active | ::1         | select datid,datname,pid,usename,state,client_addr,query from pg_stat_activity;
 16394 | lib1     | 13592 | postgres | idle   | 127.0.0.1   | SELECT id,name,age,email FROM guser WHERE id=$1
 16394 | lib1     | 11140 | postgres | idle   | 127.0.0.1   | SET application_name = 'PostgreSQL JDBC Driver'
 16394 | lib1     | 14976 | postgres | idle   | 127.0.0.1   | SET application_name = 'PostgreSQL JDBC Driver'
 16394 | lib1     |  9508 | postgres | idle   | 127.0.0.1   | SET application_name = 'PostgreSQL JDBC Driver'
 16394 | lib1     |  6728 | postgres | idle   | 127.0.0.1   | SET application_name = 'PostgreSQL JDBC Driver'
 16394 | lib1     | 12988 | postgres | idle   | 127.0.0.1   | SET application_name = 'PostgreSQL JDBC Driver'
 16394 | lib1     | 15288 | postgres | idle   | 127.0.0.1   | SET application_name = 'PostgreSQL JDBC Driver'
 16394 | lib1     | 12872 | postgres | idle   | 127.0.0.1   | SET application_name = 'PostgreSQL JDBC Driver'
 16394 | lib1     | 13108 | postgres | idle   | 127.0.0.1   | SET application_name = 'PostgreSQL JDBC Driver'
 16394 | lib1     | 19376 | postgres | idle   | 127.0.0.1   | SET application_name = 'PostgreSQL JDBC Driver'
       |          | 14144 |          |        |             |
       |          |  9304 |          |        |             |
       |          |   992 |          |        |             |
(16 行記錄)

注意,其中有10個idle的連接,這個來自本機的一個Spring Boot程序。默認的數據庫攔截池使用 HikariCP,十個連接。

 

更換爲Druid數據庫後:配置了更少的初始數據庫連接(當然,HikariCP也可以配置),,這次只有8個了

lib2=# select datid,datname,pid,usename,state,client_addr,query from pg_stat_activity;
 datid | datname |  pid  | usename  | state  | client_addr |                                      query
-------+---------+-------+----------+--------+-------------+---------------------------------------------------------------------------------
       |         | 18904 |          |        |             |
       |         | 18556 | postgres |        |             |
 16402 | lib2    | 15936 | postgres | active | ::1         | select datid,datname,pid,usename,state,client_addr,query from pg_stat_activity;
 16394 | lib1    |  2940 | postgres | idle   | 127.0.0.1   | SELECT id,name,age,email FROM guser WHERE id=$1
 16402 | lib2    | 18808 | postgres | idle   | 127.0.0.1   | SELECT id,sn,name FROM device WHERE id=$1
       |         | 14144 |          |        |             |
       |         |  9304 |          |        |             |
       |         |   992 |          |        |             |
(8 行記錄)

 

數據備份與恢復

方式1:導出sql文件,導入也是sql文件

文本文件導入導出使用 psql

# 導出數據庫 lib1
D:\>pg_dump -U postgres -f lib1 lib1
口令:

# 將數據庫lib1導出才 文本文件 導入數據庫 lib3
D:\>psql -U postgres -d lib3 -f lib1
...內容會很多...

方式2:dump文件導出導入 -Fc

D:\>pg_dump -U postgres -Fc lib1 > lib1.dump
口令:

D:\>pg_restore -U postgres -d lib3 lib1.dump > a.txt
口令:

方式3:tar文件導出導入 -Ft

D:\>pg_dump -U postgres -Ft lib1 > lib1.tar
口令:

D:\>
D:\>pg_restore -U postgres -d lib3 lib1.tar > a.txt
口令:

 

注意,

1、方式1執行時,使用 版本10的 psql從 版本13 的 PostgreSQL服務器獲取數據時,失敗了;

後安裝了 PostgreSQL 14,就可以從 低版本13 中導出數據了。

2、方式2、3中,導入時的 信息都被 重定向到了 a.txt 文件。

 

用戶&角色管理

todo

 

 

 

參考文檔

1、postgresql數據庫的數據導出

2、postgresql 查看數據庫連接數

3、

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章