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脚本
相关推荐
IT成长日记3 小时前
【MySQL基础】聚合函数从基础使用到高级分组过滤
数据库·mysql·聚合函数
Guarding and trust5 小时前
python系统之综合案例:用python打造智能诗词生成助手
服务器·数据库·python
夜间出没的AGUI5 小时前
SQLiteBrowser 的详细说明,内容结构清晰,涵盖核心功能、使用场景及实用技巧
数据库
不再幻想,脚踏实地6 小时前
MySQL(一)
java·数据库·mysql
Tyler先森7 小时前
Oracle数据库数据编程SQL<3.5 PL/SQL 存储过程(Procedure)>
数据库·sql·oracle
KevinRay_8 小时前
从零开始学习SQL
数据库·学习·mysql
Json_181790144808 小时前
python采集淘宝拍立淘按图搜索API接口,json数据示例参考
服务器·前端·数据库
Albert Tan8 小时前
Oracle 10G DG 修复从库-磁盘空间爆满导致从库无法工作
数据库·oracle
好记忆不如烂笔头abc8 小时前
oracle-blob导出,在ob导入失败
大数据·数据库·python