PostgreSQL psql 常用命令

今天总结下 PostgreSQL 中常用 psql 常用命令以便后续查阅。

角色查看

hrdb=> \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 admin     | Create role, Create DB, Cannot login                       | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 wdh       | Create DB                                                 +| {}
           | Password valid until 2060-12-31 00:00:00+08                | 

对应sql

select * from pg_roles

数据库查看

复制代码
hrdb=> \l
                                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges   
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 hrdb      | wdh      | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
复制代码

对应sql

select * from pg_database;

模式查看

hrdb=> \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 hr     | wdh
 public | pg_database_owner
(2 rows)

对应sql

select * from pg_namespace;

表结构查看

复制代码
hrdb=> \d jobs;
                         Table "public.jobs"
   Column   |         Type          | Collation | Nullable | Default 
------------+-----------------------+-----------+----------+---------
 job_id     | character varying(10) |           | not null | 
 job_title  | character varying(35) |           | not null | 
 min_salary | integer               |           |          | 
 max_salary | integer               |           |          | 
Indexes:
    "job_id_pk" PRIMARY KEY, btree (job_id)
Referenced by:
    TABLE "employees" CONSTRAINT "emp_job_fk" FOREIGN KEY (job_id) REFERENCES jobs(job_id)
复制代码

表空间查看

hrdb=> \db
       List of tablespaces
    Name    |  Owner   | Location 
------------+----------+----------
 pg_default | postgres | 
 pg_global  | postgres | 

 

posted @   晓枫的春天  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2022-07-07 基本处理函数(ProcessFunction)
点击右上角即可分享
微信分享提示