Postgresql基础命令

本文主要介绍psql命令行连接Postgresql服务器的常用命令

1.查看数据库: \l

2.列出表:\d

3.创建数据库:CREATE DATABASE pg_test;

sql 复制代码
postgres=# CREATE DATABASE pg_test;
CREATE DATABASE

4.连接创建的数据库:\c pg_test;

sql 复制代码
postgres=# \c pg_test;
You are now connected to database "pg_test" as user "postgres"

5.常用psql连接数据库的语法:psql -h <hostname or ip> -p <端口> [数据库名称] [用户名称]

sql 复制代码
psql -h 127.0.0.1 -p 5432 pg_test postgres
  1. \d 表名 显示表的结构定义,例如: \d student
sql 复制代码
pg_test-# \d student
                        Table "public.student"
 Column |  Type  | Collation | Nullable |           Default
--------+--------+-----------+----------+------------------------------
 id     | bigint |           | not null | generated always as identity
 name   | "char" |           |          |

7.\d+显示更详细信息,例如:\d+ student

sql 复制代码
pg_test-# \d+ student
                                            Table "public.student"
 Column |  Type  | Collation | Nullable |           Default            | Storage | Stats target | Description
--------+--------+-----------+----------+------------------------------+---------+--------------+-------------
 id     | bigint |           | not null | generated always as identity | plain   |              |
 name   | "char" |           |          |                              | plain   |              |
Access method: heap

8.列出所有schema

sql 复制代码
pg_test-# \dn
  List of schemas
  Name  |  Owner
--------+----------
 public | postgres
(1 row)

9.列出数据库中所有角色和用户,使用命令:\du 或者 \dg

sql 复制代码
pg_test-# \dg
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 odoo12    | Create role, Create DB                                    +| {}
           | Password valid until 2027-11-13 00:00:00+08                |
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}


pg_test-# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 odoo12    | Create role, Create DB                                    +| {}
           | Password valid until 2027-11-13 00:00:00+08                |
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {
  1. \dp 或 \z 用于列出表的权限分配情况
sql 复制代码
pg_test-# \z
                                   Access privileges
 Schema |      Name      |   Type   | Access privileges | Column privileges | Policies
--------+----------------+----------+-------------------+-------------------+----------
 public | student        | table    |                   |                   |
 public | student_id_seq | sequence |                   |                   |
(2 rows)


pg_test-# \dp
                                   Access privileges
 Schema |      Name      |   Type   | Access privileges | Column privileges | Policies
--------+----------------+----------+-------------------+-------------------+----------
 public | student        | table    |                   |                   |
 public | student_id_seq | sequence |                   |                   |
(2 rows)

11.使用\encoding 显示客户端的字符编码,如:\encoding gbk; \encoding utf8;

12.\x命令将表中的数据拆分成单行展示,类似于mysql中\G;

  1. \i a.sql 执行a.sql脚本
相关推荐
十八旬1 小时前
苍穹外卖项目实战(day7-1)-缓存菜品和缓存套餐功能-记录实战教程、问题的解决方法以及完整代码
java·数据库·spring boot·redis·缓存·spring cache
要一起看日出2 小时前
MVCC-多版本并发控制
数据库·mysql·mvcc
Hx__2 小时前
MySQL InnoDB 的 MVCC 机制
数据库·mysql
速易达网络2 小时前
ASP.NET MVC 连接 MySQL 数据库查询示例
数据库·asp.net·mvc
玉衡子3 小时前
MySQL基础架构全面解析
数据库·后端
梦中的天之酒壶3 小时前
Redis Stack扩展功能
数据库·redis·bootstrap
emma羊羊3 小时前
【 SQL注入漏洞靶场】第二关文件读写
sql·网络安全·靶场·sql注入
GreatSQL3 小时前
GreatSQL分页查询优化案例实战
数据库
Leo.yuan4 小时前
不同数据仓库模型有什么不同?企业如何选择适合的数据仓库模型?
大数据·数据库·数据仓库·信息可视化·spark
麦兜*4 小时前
MongoDB 6.0 新特性解读:时间序列集合与加密查询
数据库·spring boot·mongodb·spring·spring cloud·系统架构