Tips
重启 PostgreSQL 进程
1
| pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
|
连接
psql -U 用户名 -d 数据库名 -h 服务器地址(127.0.0.1) -p 端口(5432)
列出相关信息
\du - 显示所有用户信息
\l - 显示所有数据库信息
\q - 退出 psql 环境
新建用户并设置密码
create user 用户名 with password ‘密码‘;
1
| create user test with password 'Test123';
|
修改用户密码
alter user 用户名 with password ‘密码‘;
1
| alter user postgres with password 'Admin123';
|
新建数据库
create database 数据库名 owner 拥有者;
1
| create database testdb owner test;
|
授予用户数据库权限
grant all privileges on database 数据库名 to 拥有者;
1
| grant all privileges on database testdb to test;
|
授予用户表权限
\connect 数据库名
grant all privileges on all tables in schema public to 用户名;
1 2
| \connect test grant all privileges on all tables in schema public to test;
|
授予用户创建数据库的权限
alter user 用户名 createdb;
1
| alter user test createdb;
|
更改数据库拥有者
alter database 数据库名 owner to 新用户用户名;
1
| alter database test owner to test;
|
更改数据表拥有者
1
| alter table all tables in schema public owner to test;
|
删除数据库
drop database 数据库名;
数据库重命名
alter database 原名称 rename to 新名称;
1
| alter database test rename to test_2;
|