1、命令行登錄數(shù)據(jù)庫(kù)
有兩種方式,一是直接在系統(tǒng)shell下執(zhí)行psql命令;而是先進(jìn)入psql環(huán)境,然后再連接數(shù)據(jù)庫(kù)。下面分別給出實(shí)例:
(1)直接登錄
執(zhí)行命令:psql -h 172.16.35.179 -U username -d dbname ,其中username為數(shù)據(jù)庫(kù)用戶名,dbname為要連接的數(shù)據(jù)庫(kù)名,執(zhí)行后提示輸入密碼如下:
Password for user username: (在此輸入密碼)
輸入密碼后即可進(jìn)入psql環(huán)境了。
(2)切換數(shù)據(jù)庫(kù)
有時(shí)候需要在psql環(huán)境下切換數(shù)據(jù)庫(kù),此時(shí)執(zhí)行如下psql命令:
/c dbname username serverIP port
其中除了數(shù)據(jù)庫(kù)名外,其他的參數(shù)都是可選的,如果使用默認(rèn)值可以使用-作為占位符
執(zhí)行這個(gè)命令后,也是提示輸入密碼。
2、查看幫助
psql提供了很好的在線幫助文檔,總?cè)肟诿钍莌elp,輸入這個(gè)命令就可以看到
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特有命令的幫助是分開(kāi)的。輸入/?查看psql命令,會(huì)發(fā)現(xiàn)所有的psql命令都是以/開(kāi)頭,這就很容易和標(biāo)準(zhǔn)的SQL命令進(jìn)行區(qū)分開(kāi)來(lái)。
3、常用命令
為了便于記憶,這里把對(duì)應(yīng)的mysql命令也列出來(lái)了。
(1)列出所有的數(shù)據(jù)庫(kù)
mysql: show databases
psql: /l或/list
(2)切換數(shù)據(jù)庫(kù)
mysql: use dbname
psql: /c dbname
(3)列出當(dāng)前數(shù)據(jù)庫(kù)下的數(shù)據(jù)表
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