PostgreSql新手必學入門命令小結,postgresql小結

來源:互聯網
上載者:User

PostgreSql新手必學入門命令小結,postgresql小結

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

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.