數據庫操作SQL
創建
- 創建數據庫
create database db_test;
- 創建并指定相關參數
with
- owner : 所有者
- encoding : 編碼
- connection limit :連接限制
create database db_test1 with owner = postgresencoding = 'utf-8'connection limit = 100;
修改
- 修改數據庫名稱
rename
alter database db_test2 rename to db_test;
刪除
- 刪除數據庫
drop database if exists test1;
查看
- 查看當前數據庫
select current_database();
- 查看所有數據庫
select datname from pg_database;
- 查看數據庫所有者
select datname, pg_get_userbyid(datdba) as owner from pg_database where datname = 'db_test1';
修改所有者
- 創建角色(用戶)
create role test with login password 'test';
- 修改密碼
alter role test with password 'test1';
- 刪除用戶
drop role username;
注:如果無法刪除,需要先撤銷用戶的所有權限才能進行刪除
- 授權用戶
grant privilege_name on object_name to username;
# 授權數據庫grant all privileges on database db_ttest1 to test;# 授權表權限(需要管理員進入數據庫進行授權)grant all privileges on all tables in schema public to test;
- 撤銷授權
revoke privilege_name on object_name from username;
# 撤銷數據庫權限revoke all privileges on database db_ttest1 from test;# 撤銷表權限revoke all privileges on all tables in schema public from test;
- 修改所有者
alter database db_test2 owner to test;