溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PostgreSQL常用命令有哪些

發(fā)布時間:2021-11-02 17:00:48 來源:億速云 閱讀:169 作者:小新 欄目:MySQL數(shù)據(jù)庫

這篇文章主要介紹PostgreSQL常用命令有哪些,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

查看版本信息

mydb=# SELECT version();
                                                version                                                
-------------------------------------------------------------------------------------------------------
 PostgreSQL 9.3.6 on i686-pc-linux-gnu, compiled by gcc (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3), 32-bit
(1 row)

創(chuàng)建數(shù)據(jù)庫

mydb=# CREATE DATABASE test;
CREATE DATABASE

顯示所有數(shù)據(jù)庫

mydb-# \l
                                List of databases
   Name    |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges  
-----------+---------+----------+-------------+-------------+---------------------
 mydb      | postgre | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 postgres  | postgre | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgre | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgre         +
           |         |          |             |             | postgre=CTc/postgre
 template1 | postgre | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgre         +
           |         |          |             |             | postgre=CTc/postgre
 test      | postgre | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
(5 rows)

切換當前數(shù)據(jù)庫

mydb-# \c test
Password for user postgre: 
You are now connected to database "test" as user "postgre".

刪除數(shù)據(jù)庫

# 不能刪除當前連接的數(shù)據(jù)庫
test=# DROP DATABASE test;            
ERROR:  cannot drop the currently open database

test=# DROP DATABASE mydb;
DROP DATABASE

列舉當前數(shù)據(jù)庫表

test=# create table t as select * from pg_tablespace;
SELECT 2

test=# \dt
        List of relations
 Schema | Name | Type  |  Owner  
--------+------+-------+---------
 public | t    | table | postgre
(1 row)

查看表結(jié)構(gòu)

test=# \d t
          Table "public.t"
   Column   |   Type    | Modifiers 
------------+-----------+-----------
 spcname    | name      | 
 spcowner   | oid       | 
 spcacl     | aclitem[] | 
 spcoptions | text[]    |

重命名表

test=# alter table t rename to t_t;
ALTER TABLE

test=# \dt
        List of relations
 Schema | Name | Type  |  Owner  
--------+------+-------+---------
 public | t_t  | table | postgre
(1 row)

查看表索引

# 創(chuàng)建索引
test=# create index idx_name on t (spcname);
CREATE INDEX

# 顯示索引
test=# \di ;
              List of relations
 Schema |   Name   | Type  |  Owner  | Table 
--------+----------+-------+---------+-------
 public | idx_name | index | postgre | t
 public | idx_t    | index | postgre | t_t
(2 rows)

刪除表

test=# drop table t_t;
DROP TABLE

創(chuàng)建用戶

test=# CREATE USER svoid;
CREATE ROLE
# 創(chuàng)建sovid 指定密碼
test=# CREATE USER svoid WITH PASSWORD '123qwe';
CREATE ROLE

查看系統(tǒng)用戶信息

test=# SELECT usename FROM pg_user;
 usename 
---------
 postgre
 svoid
(2 rows)

test=# \du;
                             List of roles
 Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
 postgre   | Superuser, Create role, Create DB, Replication | {}
 svoid     |                                                | {}

刪除用戶

test=# DROP USER svoid;
DROP ROLE

查看schema

test=# \dn;
 List of schemas
  Name  |  Owner  
--------+---------
 public | postgre
(1 row)

創(chuàng)建schema

test=# CREATE SCHEMA myschema;
CREATE SCHEMA

刪除schema

test=# DROP SCHEMA myschema;
DROP SCHEMA

以上是“PostgreSQL常用命令有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI