PostgreSQL(二)常用命令整理

一、常用命令

登录命令:

psql -h IP -p 端口 -U 用户名 -d 数据库名 -W
  • \? 所有命令帮助
  • \l 列出所有数据库
  • \d 列出数据库中所有表
  • \dt 列出数据库中所有表
  • \d [table_name] 显示指定表的结构
  • \di 列出数据库中所有 index
  • \dv 列出数据库中所有 view
  • \h sql命令帮助
  • \q 退出连接
  • \c [database_name] 切换到指定的数据库
  • \c 显示当前数据库名称和用户
  • \conninfo 显示客户端的连接信息
  • \du 显示所有用户
  • \dn 显示数据库中的schema
  • \encoding 显示字符集
  • select version(); 显示版本信息

二、用户

2.1 创建账号

创建用户:

create user 用户名 password '密码';

设置只读权限:

alter user 用户名 set default_transaction_read_only = on;

设置可操作的数据库:

grant all on database 数据库名 to 用户名;

授权可操作的模式和权限:

-- 授权
grant select on all tables in schema public to 用户名;
-- 授权
GRANT ALL ON TABLE public.user TO mydata;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE public.user TO mydata_dml;
GRANT SELECT ON TABLE public.user TO mydata_qry;

2.2 删除账号

撤回在public模式下的权限:

revoke select on all tables in schema public from 用户名;

撤回在information_schema模式下的权限:

revoke select on all tables in schema information_schema from 用户名;

撤回在pg_catalog模式下的权限:

revoke select on all tables in schema pg_catalog from 用户名;

撤回对数据库的操作权限:

revoke all on database 数据库名 from 用户名;

删除用户:

drop user 用户名;

三、权限

3.1 授权

设置只读权限:

alter user 用户名 set default_transaction_read_only = on;

设置可操作的数据库:

grant all on database 数据库名 to 用户名;

设置可操作的模式和权限:

grant select,insert,update,delete on all tables in schema public to 用户名;

2.2 撤回权限

撤回在public模式下的权限:

revoke select on all tables in schema public from 用户名;

撤回在information_schema模式下的权限:

revoke select on all tables in schema information_schema from 用户名;

撤回在pg_catalog模式下的权限:

revoke select on all tables in schema pg_catalog from 用户名;

撤回对数据库的操作权限:

revoke all on database 数据库名 from 用户名;

四、模式 Schema

创建和当前用户同名模式(schema):

create schema AUTHORIZATION CURRENT_USER;

自定义创建模式(schema):

create schema 模式名称;

注意:如果不创建scheme,并且语句中不写scheme,则默认scheme使用内置的public

查看数据库下的所有(schema):

select * from information_schema.schemata;

五、数据库

查询所有数据库:

select datname from pg_database;

创建数据库:

create database 数据库名 owner 所属用户 encoding UTF8;

删除数据库:

drop database 数据库名;

六、表

查询schema中所有表:

select table_name from information_schema.tables where table_schema = 'mobileuser';

创建表:

CREATE TABLE public.t_user (
  "id" BIGSERIAL NOT NULL,
  "username" VARCHAR(64) NOT NULL,
  "password" VARCHAR(64) NOT NULL,
  "create_time" TIMESTAMP(0) default CURRENT_TIMESTAMP not null,
  "update_time" TIMESTAMP(0) default CURRENT_TIMESTAMP not null
);
-- 注释
COMMENT ON TABLE public.t_user IS '用户表';
COMMENT ON COLUMN public.t_user.id IS '主键';
COMMENT ON COLUMN public.t_user.username IS '用户名';
COMMENT ON COLUMN public.t_user.password IS '密码';
COMMENT ON COLUMN public.t_user.create_time IS '创建时间';
COMMENT ON COLUMN public.t_user.update_time IS '更新时间';
-- 创建自增序列
alter sequence "t_user_ID_seq" restart with 1 increment by 1;
-- 创建主键序列
drop index if exists "t_user_pkey";
alter table "t_user" add constraint "t_user_pkey" primary key ("ID");

删除表:

drop table if exists "t_template" cascade;

七、索引

创建索引:

drop index if exists t_user_username;
create index t_user_username on t_user (username);

创建唯一索引:

drop index if exists t_user_username;
create index t_user_username on t_user (username);

五、查询SQL

注意:PostgreSQL中的字段大小写敏感,而且只认小写字母,查询时需注意。

其他与基本sql大致相同。

根据时间查询:

select * from t_user
where create_time >= to_timestamp('2023-01-01 00:00:00', 'yyyy-mm-dd hh24:MI:SS');

查询时间格式化:

select to_char(create_time, 'yyyy-mm-dd hh24:MI:SS') from t_user;





参考地址:

1.PostgreSql常用命令,https://blog.csdn.net/weixin_48321825/article/details/121775011

2.Postgresql创建账号及删除账号详细命令,https://blog.csdn.net/qq_44322586/article/details/123084962

猜你喜欢

转载自blog.csdn.net/qq_33204709/article/details/128772173