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)

创建数据库

mydb=# CREATE DATABASE test;
CREATE DATABASE

显示所有数据库

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)

切换当前数据库

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

删除数据库

# 不能删除当前连接的数据库
test=# DROP DATABASE test;            
ERROR:  cannot drop the currently open database

test=# DROP DATABASE mydb;
DROP DATABASE

列举当前数据库表

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)

查看表结构

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)

查看表索引

# 创建索引
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

创建用户

test=# CREATE USER svoid;
CREATE ROLE
# 创建sovid 指定密码
test=# CREATE USER svoid WITH PASSWORD '123qwe';
CREATE ROLE

查看系统用户信息

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)

创建schema

test=# CREATE SCHEMA myschema;
CREATE SCHEMA

删除schema

test=# DROP SCHEMA myschema;
DROP SCHEMA


待补充。。。

延伸阅读:
http://www.cnblogs.com/stephen-liu74/archive/2012/06/08/2315679.html

整理自网络

Svoid
2015-04-20

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29733787/viewspace-1588232/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29733787/viewspace-1588232/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值