1. 程式人生 > >postgresql基本命令操作

postgresql基本命令操作

postgresql基本命令操作

postgresql基本命令操作:

登陸數據庫:

[[email protected] ~]$ psql -Utestwjw -h 127.0.0.1 -dpostgres -p 36985

Password for user testwjw:

psql.bin (9.5.9)

Type "help" for help.


postgres=>

切換數據庫:

postgres=> \c testdb1

You are now connected to database "testdb1" as user "testwjw".

查看所有的數據庫:

testdb1=> \l

testdb1=> \list

List of databases

Name | Owner | Encoding | Collate | Ctype | Access privileges

-----------+----------+----------+-------------+-------------+-----------------------

postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |

template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +

| | | | | postgres=CTc/postgres

template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +

| | | | | postgres=CTc/postgres

testdb1 | testwjw | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/testwjw +

| | | | | testwjw=CTc/testwjw

testdb2 | testwjw | UTF8 | en_US.UTF-8 | en_US.UTF-8 |

(5 rows)


查看所有的表:

testdb1=> \dt

List of relations

Schema | Name | Type | Owner

--------+-------+-------+---------

public | t | table | testwjw

public | t1 | table | testwjw

public | tlb01 | table | testwjw

(3 rows)


testdb1=>


創建數據庫:

[[email protected] ~]$ psql -p 36985

psql.bin (9.5.9)

Type "help" for help.


postgres=# create database testdb3 with encoding=‘utf8‘ owner=testwjw;

CREATE DATABASE


[[email protected] ~]$ createdb testdb5 -p 36985

[[email protected] ~]$ createdb testdb6 -p 36985


查看創建的數據庫:

[[email protected] ~]$ psql -p 36985 -c ‘\list‘|egrep "testdb4|testdb5"

testdb4 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |

testdb5 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |


刪除創建的數據庫:

#以testwjw的身份連接服務器,刪除testdb1數據庫。

[[email protected] ~]$ dropdb -Utestwjw -p 36985 -e testdb1

DROP DATABASE testdb1;

[[email protected] ~]$ psql -p 36985 -c ‘\list‘|grep "testdb1"

通過查看系統表驗證該數據庫是否已經被刪除:

[[email protected] ~]$ psql -p 36985 -c "SELECT count(*) FROM pg_database WHERE datname =‘testdb1‘"

count

-------

0

(1 row)


證明此數據庫確實被刪除。




查看數據庫中所有的表以及單表結構:

testdb2=# \dt

List of relations

Schema | Name | Type | Owner

--------+------+-------+---------

public | tlb2 | table | testwjw

(1 row)


testdb2=# \d tlb2

Table "public.tlb2"

Column | Type | Modifiers

--------+-----------------------+-----------

id | integer |

pay | character varying(20) |

name | character varying(6) |

Indexes:

"uniq" UNIQUE CONSTRAINT, btree (id)


testdb2=#

查看索引詳細信息:

testdb2=# \d uniq;

Index "public.uniq"

Column | Type | Definition

--------+---------+------------

id | integer | id

unique, btree, for table "public.tlb2"


\d+ 命令:將會顯示比\d命令更詳細的信息,除了前面介紹的那些,它還會顯示任何與表列相關的註釋,以及表中出現的OID。

testdb2=# \d+

List of relations

Schema | Name | Type | Owner | Size | Description

--------+------+-------+---------+---------+-------------

public | tlb2 | table | testwjw | 0 bytes |

(1 row)


testdb2=# \d

List of relations

Schema | Name | Type | Owner

--------+------+-------+---------

public | tlb2 | table | testwjw

(1 row)


testdb2=#


列出所有的schemas:


testdb2=# \dn

List of schemas

Name | Owner

--------+----------

public | postgres

(1 row)


創建schema:

testdb2=# create schema sa;

CREATE SCHEMA


testdb2=# \dn

List of schemas

Name | Owner

--------+----------

public | postgres

sa | postgres

(2 rows)


testdb2=#


顯示sql執行的時間,可以使用\timing參數:

testdb2=# \timing

Timing is on.

testdb2=# select * from tlb2;

id | pay | name

----+-----+------

(0 rows)


Time: 0.177 ms

testdb2=#


如果想列出數據庫中所有的角色或者用戶,可以使用\du \dg,這兩個命令等價,因為postgresSQL中用戶和角色不區分。


testdb2=# \du

List of roles

Role name | Attributes | Member of

-----------+------------------------------------------------------------+-----------

postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

testwjw | | {}


testdb2=# \dg

List of roles

Role name | Attributes | Member of

-----------+------------------------------------------------------------+-----------

postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

testwjw | | {}


testdb2=#



查看表字段:

testdb2=# SELECT a.attname from pg_class c,pg_attribute a,pg_type t where c.relname=‘tlb2‘ and a.attnum>0 and a.attrelid=c.oid and a.atttypid=t.oid;

attname

---------

id

pay

name

(3 rows)

Time: 0.586 ms

testdb2=# \dnp+

List of schemas

Name | Owner | Access privileges | Description

--------+----------+----------------------+------------------------

public | postgres | postgres=UC/postgres+| standard public schema

| | =UC/postgres |

sa | postgres | |

(2 rows)


testdb2=# \dn

List of schemas

Name | Owner

--------+----------

public | postgres

sa | postgres

(2 rows)

testdb2=#

本文出自 “10931853” 博客,請務必保留此出處http://wujianwei.blog.51cto.com/10931853/1970402

postgresql基本命令操作