/# PGPASSWORD=liubei@161 psql -U liubei -d xishu
当然你也可以交互式输入密码
psql -U liubei -d xishu
psql (13.8 (Debian 13.8-1.pgdg110+1))
Type "help" for help.
xishu=#
xishu=# create database jing_zhou;
CREATE DATABASE
xishu=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------+----------+------------+------------+-------------------
jing_zhou | liubei | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | liubei | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | liubei | UTF8 | en_US.utf8 | en_US.utf8 | =c/liubei +
| | | | | liubei=CTc/liubei
template1 | liubei | UTF8 | en_US.utf8 | en_US.utf8 | =c/liubei +
| | | | | liubei=CTc/liubei
xishu | liubei | UTF8 | en_US.utf8 | en_US.utf8 |
(5 rows)
如上可见我们创建的数据库
jing_zhou
xishu=# \c jing_zhou;
You are now connected to database "jing_zhou" as user "liubei".
xishu=# alter database jing_zhou rename to new_jing_zhou;
xishu=# drop database new_jing_zhou;
CREATE TABLE users(
ID INT PRIMARY KEY NOT NULL,
name CHAR(50) NOT NULL,
mail CHAR(50),
phone CHAR(50)
);
xishu=# \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+--------
public | users | table | liubei
(2 rows)
xishu=# drop table users;
INSERT INTO users (ID,name,mail)
VALUES (1,'guanYu','[email protected]');
xishu=# select * from users;
id | name | mail | phone
----+----------------------------------------------------+----------------------------------------------------+-------
1 | guanYu | guanyu@xishu.com |
delete from users where name='guanYu';
CREATE USER
和REATE ROLE
的区别
CREATE USER
:默认LOGIN
CREATE ROLE
:默认NOLOGIN
CREATE USER guanyu PASSWORD 'guanyu@123456';
或者
CREATE ROLE guanyunchang LOGIN PASSWORD 'guanyu@123456';
CREATE USER kongming SUPERUSER PASSWORD 'kongming@123456';
CREATE USER pangtong CREATEDB;
CREATE USER pangtong CREATETABLE;
CREATE USER fazheng CREATEROLE;
SELECT * FROM pg_roles;
rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolbypassrls | rolconfig | oid
---------------------------+----------+------------+---------------+-------------+-------------+----------------+--------------+-------------+---------------+--------------+-----------+-------
pg_signal_backend | f | t | f | f | f | f | -1 | ******** | | f | | 4200
pg_read_server_files | f | t | f | f | f | f | -1 | ******** | | f | | 4569
guanyu | f | t | f | f | t | f | -1 | ******** | | f | | 16403
liubei | t | t | t | t | t | t | -1 | ******** | | t | | 10
pg_write_server_files | f | t | f | f | f | f | -1 | ******** | | f | | 4570
pg_execute_server_program | f | t | f | f | f | f | -1 | ******** | | f | | 4571
fazheng | f | t | t | f | t | f | -1 | ******** | | f | | 16406
pg_read_all_stats | f | t | f | f | f | f | -1 | ******** | | f | | 3375
pg_monitor | f | t | f | f | f | f | -1 | ******** | | f | | 3373
guanyunchang | f | t | f | f | t | f | -1 | ******** | | f | | 16407
pangtong | f | t | f | t | t | f | -1 | ******** | | f | | 16405
pg_read_all_settings | f | t | f | f | f | f | -1 | ******** | | f | | 3374
pg_stat_scan_tables | f | t | f | f | f | f | -1 | ******** | | f | | 3377
kongming | t | t | f | f | t | f | -1 | ******** | | f | | 16404
(14 rows)
drop user guanyunchang ;
ALTER USER guanyu RENAME TO guanyunchang;
NOTICE: MD5 password cleared because of role rename
ALTER ROLE
如上可知,密码被清空。
ALTER USER guanyunchang PASSWORD 'guanyunchang@123456';
这里不展开写了,可以对应创建用户时的权限
ALTER ROLE fazheng NOCREATEDB ;
SELECT
、INSERT
、UPDATE
、DELETE
、TRUNCATE
、REFERENCES
、TRIGGER
、CREATE
、CONNECT
、TEMPORARY
、EXECUTE
、USAGE
给用户关云长修改user表的权限
GRANT UPDATE ON users TO guanyunchang;
给所有用户查看users表的权限
GRANT SELECT ON users TO PUBLIC;
给所用户关云长user表所有权限
GRANT ALL ON users TO guanyunchang;
REVOKE DELETE ON users FROM guanyunchang ;