1. 程式人生 > >PostgreSql命令

PostgreSql命令

情況 區分 itl str lib esql face distrib lis

1 命令行登錄數據庫

有兩種方式,一是直接在系統shell下執行psql命令;而是先進入psql環境,然後再連接數據庫。下面分別給出實例:

(1)直接登錄

執行命令:psql -h 172.16.35.179 -U username -d dbname ,其中username為數據庫用戶名,dbname為要連接的數據庫名,執行後提示輸入密碼如下:
Password for user username: (在此輸入密碼)

輸入密碼後即可進入psql環境了。

(2)切換數據庫

有時候需要在psql環境下切換數據庫,此時執行如下psql命令: \c dbname username serverIP port
其中除了數據庫名外,其他的參數都是可選的,如果使用默認值可以使用-作為占位符 執行這個命令後,也是提示輸入密碼。

2 查看幫助

psql提供了很好的在線幫助文檔,總入口命令是help,輸入這個命令就可以看到 vsb9=# help
You are using psql, the command-line interface to PostgreSQL.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit 可以看到,標準SQL命令的幫助和psql特有命令的幫助是分開的。輸入\?查看psql命令,會發現所有的psql命令都是以\開頭,這就很容易和標準的SQL命令進行區分開來。

3 常用命令

為了便於記憶,這裏把對應的mysql命令也列出來了。

(1)列出所有的數據庫

mysql: show databases psql: \l或\list

(2)切換數據庫

mysql: use dbname psql: \c dbname

(3)列出當前數據庫下的數據表

mysql: show tables psql: \d

(4)列出指定表的所有字段

mysql: show columns from table name psql: \d tablename

(5)查看指定表的基本情況

mysql: describe tablename psql: \d+ tablename

(6)退出登錄

mysql: quit 或者\q psql:\q

PostgreSql命令