1、命令行登錄數(shù)據(jù)庫
有兩種方式,一是直接在系統(tǒng)shell下執(zhí)行psql命令;而是先進(jìn)入psql環(huán)境,然后再連接數(shù)據(jù)庫。下面分別給出實(shí)例:
(1)直接登錄
執(zhí)行命令:psql -h 172.16.35.179 -U username -d dbname ,其中username為數(shù)據(jù)庫用戶名,dbname為要連接的數(shù)據(jù)庫名,執(zhí)行后提示輸入密碼如下:
復(fù)制代碼 代碼如下:
Password for user username: (在此輸入密碼)
輸入密碼后即可進(jìn)入psql環(huán)境了。
(2)切換數(shù)據(jù)庫
有時(shí)候需要在psql環(huán)境下切換數(shù)據(jù)庫,此時(shí)執(zhí)行如下psql命令:
復(fù)制代碼 代碼如下:
\c dbname username serverIP port
其中除了數(shù)據(jù)庫名外,其他的參數(shù)都是可選的,如果使用默認(rèn)值可以使用-作為占位符
執(zhí)行這個(gè)命令后,也是提示輸入密碼。
2、查看幫助
psql提供了很好的在線幫助文檔,總?cè)肟诿钍莌elp,輸入這個(gè)命令就可以看到
復(fù)制代碼 代碼如下:
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
可以看到,標(biāo)準(zhǔn)SQL命令的幫助和psql特有命令的幫助是分開的。輸入\&;查看psql命令,會(huì)發(fā)現(xiàn)所有的psql命令都是以\開頭,這就很容易和標(biāo)準(zhǔn)的SQL命令進(jìn)行區(qū)分開來。
3、常用命令
為了便于記憶,這里把對(duì)應(yīng)的mysql命令也列出來了。
(1)列出所有的數(shù)據(jù)庫
復(fù)制代碼 代碼如下:
mysql: show databases
psql: \l或\list
(2)切換數(shù)據(jù)庫
復(fù)制代碼 代碼如下:
mysql: use dbname
psql: \c dbname
(3)列出當(dāng)前數(shù)據(jù)庫下的數(shù)據(jù)表
復(fù)制代碼 代碼如下:
mysql: show tables
psql: \d
(4)列出指定表的所有字段
復(fù)制代碼 代碼如下:
mysql: show columns from table name
psql: \d tablename
(5)查看指定表的基本情況
復(fù)制代碼 代碼如下:
mysql: describe tablename
psql: \d+ tablename
(6)退出登錄
復(fù)制代碼 代碼如下:
mysql: quit 或者\(yùn)q
psql:\q
您可能感興趣的文章:- PostgreSQL教程(十八):客戶端命令(2)
- PostgreSQL教程(十七):客戶端命令(1)
- 15個(gè)postgresql數(shù)據(jù)庫實(shí)用命令分享
- PostgreSQL copy 命令教程詳解