摘要
PostgreSQL的常用命令
1、登錄數(shù)據(jù)庫(kù)
/* 切換到數(shù)據(jù)庫(kù)用戶(hù) */
su - postgres
/* 登錄 */
psql
登錄成功顯示如下:
bash-4.2$ psql
psql (9.3.17)
Type "help" for help.
postgres=>
2、切換數(shù)據(jù)庫(kù)
/* 登錄指定數(shù)據(jù)庫(kù) */
psql -U user -d dbname
/* 列舉數(shù)據(jù)庫(kù) */
\l
/* 切換數(shù)據(jù)庫(kù) */
\c dbname
3、用戶(hù)管理
/* 創(chuàng)建用戶(hù) */
CREATE ROLE rolename;
CREATE USER username WITH PASSWORD '*****';
/* 顯示所有用戶(hù) */
\du
/* 修改用戶(hù)權(quán)限 */
ALTER ROLE username WITH privileges;
/* 賦給用戶(hù)表的所有權(quán)限 */
GRANT ALL ON tablename TO user;
/* 賦給用戶(hù)數(shù)據(jù)庫(kù)的所有權(quán)限 */
GRANT ALL PRIVILEGES ON DATABASE dbname TO dbuser;
/* 撤銷(xiāo)用戶(hù)權(quán)限 */
REVOKE privileges ON tablename FROM user;
/* 撤銷(xiāo)用戶(hù)權(quán)限 */
4、數(shù)據(jù)庫(kù)操作
/* 創(chuàng)建數(shù)據(jù)庫(kù) */
create database dbname;
/* 刪除數(shù)據(jù)庫(kù) */
drop database dbname;
5、表操作
/* 增加讓主鍵自增的權(quán)限 */
grant all on sequence tablename_keyname_seq to webuser;
/* 重命名一個(gè)表 */
alter table [表名A] rename to [表名B];
/* 刪除一個(gè)表 */
drop table [表名];
/* 在已有的表里添加字段 */
alter table [表名] add column [字段名] [類(lèi)型];
/* 刪除表中的字段 */
alter table [表名] drop column [字段名];
/* 重命名一個(gè)字段 */
alter table [表名] rename column [字段名A] to [字段名B];
/* 給一個(gè)字段設(shè)置缺省值 */
alter table [表名] alter column [字段名] set default [新的默認(rèn)值];
/* 去除缺省值 */
alter table [表名] alter column [字段名] drop default;
/* 插入數(shù)據(jù) */
insert into 表名 ([字段名m],[字段名n],......) values ([列m的值],[列n的值],......);
/* 修改數(shù)據(jù) */
update [表名] set [目標(biāo)字段名]=[目標(biāo)值] where ...;
/* 刪除數(shù)據(jù) */
delete from [表名] where ...;
/* 刪除表 */
delete from [表名];
/* 查詢(xún) */
SELECT * FROM dbname WHERE ...;
/* 創(chuàng)建表 */
create table (
[字段名1] [類(lèi)型1] primary key,
[字段名2] [類(lèi)型2],
......,
[字段名n] [字段名n] )
6、退出
補(bǔ)充:postgresql 授權(quán)某個(gè)數(shù)據(jù)庫(kù)的權(quán)限給test 賬號(hào) 使該賬號(hào) 只能操作指定DB 不能操作其他DB
alter user test set default_transaction_read_only=on;
grant all on database crm_db to test;
grant select on all tables in schema public to test; // 起作用的是這句 要進(jìn)入crm_db 操作,在那個(gè)db環(huán)境執(zhí)行就授哪個(gè)db的權(quán)
刪除前撤銷(xiāo)
revoke all on database crm_prod_myl from test;
revoke select on all tables in schema public from test;
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- Postgresql 賦予用戶(hù)權(quán)限和撤銷(xiāo)權(quán)限的實(shí)例
- postgresql限制某個(gè)用戶(hù)僅連接某一個(gè)數(shù)據(jù)庫(kù)的操作
- PostgreSQL 實(shí)現(xiàn)快速刪除一個(gè)用戶(hù)
- 在postgresql數(shù)據(jù)庫(kù)中創(chuàng)建只讀用戶(hù)的操作
- 查看postgresql數(shù)據(jù)庫(kù)用戶(hù)系統(tǒng)權(quán)限、對(duì)象權(quán)限的方法
- postgresql 查看當(dāng)前用戶(hù)名的實(shí)現(xiàn)