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脚本
相关推荐
向上的车轮23 分钟前
什么是向量数据库?向量数据库和关系数据库有什么区别?
数据库·向量数据库
boring_11143 分钟前
异地多活单元化架构下的微服务体系
数据库·微服务·架构
betazhou1 小时前
oracle goldengate非并行进程转换为并行进程
数据库·oracle·并行·parallel·ogg·同步数据
wuli玉shell1 小时前
数仓-范式建模、维度建模、雪花模型、星型模型对比及其适用范围
数据库·oracle
ghie90901 小时前
oracle dblink varchar类型查询报错记录
数据库·oracle
等rain亭1 小时前
MySQL数据库创建、删除、修改
数据库·mysql
怀君1 小时前
Flutter——数据库Drift开发详细教程(五)
数据库·flutter
Le_ee1 小时前
sqli-labs靶场第二关——数字型
数据库·sql·网络安全·sql注入·sqli—labs
小哈里1 小时前
【Oracle认证】MySQL 8.0 OCP 认证考试英文版(MySQL30 周年版)
数据库·mysql·ocp·oracle认证·证书考试
kingwebo'sZone2 小时前
Error parsing column 10 (YingShou=-99.5 - Double) dapper sqlite
数据库·sqlite