上篇文章介绍了如何安装Postgresql,这篇介绍一些简单命令:
上篇链接:http://blog.csdn.net/badly9/article/details/48544459
1.psql命令简介:
首先登入Postgresql控制台:
[pg@edb1~]$psqlpostgres
psql(9.3.9)
Type"help"forhelp.
postgres=#
查看帮助:
postgres=#\h
Availablehelp:
ABORTCLUSTERDEALLOCATEEND
ALTERAGGREGATECOMMENTDECLAREEXECUTE
ALTERCOLLATIONCOMMITDELETEEXPLAIN
.....
这里会列出所有能执行的命令:
如果需要某个命令具体的帮助还可以使用:
postgres=#\hcreateuser
Command:CREATEUSER
Description:defineanewdatabaserole
CREATEUSERname[[WITH]option[...]]
whereoptioncanbe:
SUPERUSER|NOSUPERUSER
|CREATEDB|NOCREATEDB
|CREATEROLE|NOCREATEROLE
|CREATEUSER|NOCREATEUSER
|INHERIT|NOINHERIT
|LOGIN|NOLOGIN
|REPLICATION|NOREPLICATION
|CONNECTIONLIMITconnlimit
|[ENCRYPTED|UNENCRYPTED]PASSWORD'password'
|VALIDUNTIL'timestamp'
|INROLErole_name[,...]
|INGROUProle_name[,192)">|ROLErole_name[,192)">|ADMINrole_name[,192)">|USERrole_name[,192)">|SYSIDuid
(1)Postgresql控制台创建
创建用户:
postgres=#createuserbadly9encryptedpassword'badly9';
CREATEROLE
创建数据库
postgres=#createdatabasebadly9db;
CREATEDATABASE
授予权限
postgres=#grantallondatabasebadly9dbtobadly9;
GRANT
(2)命令行创建
创建用户:
[pg@edb1~]$createuser--help--可以用来查看help,输出太多就不贴了
[pg@edb1~]$createuser-W-Ebadly9
Password:
创建属于badly9的数据库badly9db:
[pg@edb1~]$createdb-Obadly9badly9db
[pg@edb1~]$psql-dbadly9db-h127.0.0.1-p5432-Ubadly9-W
Passwordforuserbadly9:
badly9db=>
上边登陆的含义是使用用户badly9登陆ip地址为127.0.0.1监听端口为5432的数据库badly9db
4.简单的DDL、DML及查询测试:
(1)DDL测试:
badly9db=>createtablebadly9(xint);
CREATETABLE
badly9db=>altertablebadly9addyint;
ALTERTABLE
badly9db=>altertablebadly9dropcolumny;
badly9db=>altertablebadly9renametobadly10;
badly9db=>droptablebadly10;
DROPTABLE
(2)DML及查询测试
badly9db=>createtablebadly9(xvarchar(20));
badly9db=>insertintobadly9values('who');
INSERT01
badly9db=>select*frombadly9;
x
-----
who
(1row)
badly9db=>updatebadly9setx='badly'wherex='who';
UPDATE1
-------
badly
badly9db=>deletefrombadly9wherex='badly';
DELETE1
---
(0rows)
badly9=#\q
(1)Postgresql控制台
postgres=#dropdatabasebadly9db;
DROPDATABASE
(2)命令行
[pg@edb1~]$dropdbbadly9db
(1)Postgresql控制台
postgres=#dropuserbadly9;
DROPROLE
(2)命令行
[pg@edb1~]$dropuserbadly9
原文链接:https://www.f2er.com/postgresql/194784.html