PostgreSQL快速入门:psql工具的使用_MySQL

老伟姑娘_7274

老伟姑娘_7274

2016-05-27

6658人浏览

原创

win可用的mysql指令skill
win可用的mysql指令skill

安装、修复、更新或打包一个基于 Python 和 PyMySQL 实现的 Windows `mysql` 命令。适用于用户希望在 PowerShell/CMD 中输入 `mysql` 时重定向到 Python 实现、需要交互式输入 MySQL 连接信息、需要同步到当前用户本地 bin 目录,或需要将该能力封装成可公开复用的 Codex skill。

下载

一、psql介绍

psql是PostgreSQL中的一个命令行交互式客户端工具,类似Oracle中的命令行工具sqlplus:

1.允许你交互地键入SQL或命令,然后把它们发出给PostgreSQL服务器,再显示SQL或命令的结果;

2.输入的内容还可以来自一个文件;

3.还提供了一些元命令和多种类似shell的特性来实现书写脚本,以及对对量任务的自动化工作;

二、psql的简单实用

按照前面的步骤,切换su - postgres用户,实用psql工具连接数据库。

1.查看有哪些数据库

postgres=# \l 
                                  List of databases 
   Name    |  Owner   | Encoding |  Collation  |   Ctype   |  Access privileges  
-----------+----------+----------+-------------+-------------+----------------------- 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |  
 template0 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres 
 template1 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres 
(3 rows)

a.安装好后,默认会有一个叫postgres的数据库,还有两个模板数据库template0和template1;

b.用户再建数据库的时候,默认是从模板数据库template1克隆出来;

c.template0是一个最简化的模板库,创建数据库时,如果明确指定从此数据库集成,将创建一个最简化的数据库;

2.创建数据库osdba

postgres=# CREATE DATABASE osdba; 
CREATE DATABASE 
postgres-# \l 
                                  List of databases 
   Name    |  Owner   | Encoding |  Collation  |    Ctype    |  Access privileges
-----------+----------+----------+-------------+-------------+----------------------- 
 osdba     | postgres | UTF8    | en_US.UTF-8 | en_US.UTF-8 |  
 postgres  | postgres | UTF8    | en_US.UTF-8 | en_US.UTF-8 |  
 template0 | postgres | UTF8   | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres 
 template1 | postgres | UTF8   | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres 
(4 rows)

3.访问osdba数据库

-bash-4.1$ psql osdba 
psql (8.4.20) 
Type "help" for help.

4.在数据库osdba中创建表t

osdba=# create table t(id int primary key,name varchar(40)); 
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t_pkey" for table "t" 
CREATE TABLE

5.查看osdb数据库中的表

osdba=# \d 
        List of relations 
 Schema | Name | Type  |  Owner    
--------+------+-------+---------- 
 public | t    | table | postgres 
(1 row)

6.创建数据库testdb后,并连接到testdb数据库

osdba=# CREATE DATABASE testdb; 
CREATE DATABASE 
osdba=# \c testdb 
psql (8.4.20) 
You are now connected to database "testdb". 
testdb=#

三、psql的常用命令

1.\d命令-查看当前数据库中的所有表

osdba-# \d 
        List of relations 
 Schema | Name | Type  |  Owner    
--------+------+-------+---------- 
 public | t    | table | postgres 
(1 row)

2.\d命令-跟一个表命,查看这个表的结构定义

osdba-# \d t 
              Table "public.t" 
 Column |         Type          | Modifiers  
--------+-----------------------+----------- 
 id     | integer               | not null 
 name   | character varying(40) |  
Indexes: 
    "t_pkey" PRIMARY KEY, btree (id)

3.\d命令-可以查看表格t的索引信息

osdba-# \d t_pkey 
Index "public.t_pkey" 
 Column |  Type    
--------+--------- 
 id     | integer 
primary key, btree, for table "public.t"

4.\d命令-跟通配符如*或?

osdba-# \d t* 
              Table "public.t" 
 Column |         Type          | Modifiers  
--------+-----------------------+----------- 
 id     | integer               | not null 
 name   | character varying(40) |  
Indexes: 
    "t_pkey" PRIMARY KEY, btree (id) 
 
Index "public.t_pkey" 
 Column |  Type    
--------+--------- 
 id     | integer 
primary key, btree, for table "public.t"

5.\d+命令,显示比\d命令更加详细的信息,显示与表列关联的注释

osdba-# \d+ 
                    List of relations 
 Schema | Name | Type  |  Owner   |  Size   | Description  
--------+------+-------+----------+---------+------------- 
 public | t    | table | postgres | 0 bytes |  
(1 row)

6.匹配不同对象类型的\d命令,如\dt-只想显示匹配的表;\di-只想显示索引;\ds-只显示序列;\dv-只显示视图;\df-只显示函数等...

osdba-# \dt t* 
        List of relations 
 Schema | Name | Type  |  Owner    
--------+------+-------+---------- 
 public | t    | table | postgres

7.\dn命令-列出所有的schema

osdba-# \dn 
        List of schemas 
        Name        |  Owner    
--------------------+---------- 
 information_schema | postgres 
 pg_catalog         | postgres 
 pg_toast_temp_1    | postgres 
 public             | postgres 
(5 rows)

8.\db命令-显示所有表空间

osdba-# \db 
       List of tablespaces 
    Name    |  Owner   | Location  
------------+----------+---------- 
 pg_default | postgres |  
 pg_global  | postgres |  
(2 rows)

9.\dg-列出数据库中所有角色和用户

osdba-# \dg 
            List of roles 
 Role name | Attributes  | Member of  
-----------+-------------+----------- 
 postgres  | Superuser   | {} 
           : Create role    
           : Create DB

10.\dp-显示表的权限分配情况

osdba-# \dp 
                          Access privileges 
 Schema | Name | Type  | Access privileges | Column access privileges  
--------+------+-------+-------------------+-------------------------- 
 public | t    | table |                   |  
(1 row)

四、指定字符集编译的命令

1.\encoding gbk-设置客户端的字符编码为gbk

五、\pset命令

1.\pset命令-用于设置输出的格式,\pset border 0/1/2:输出内容无边框、只有内边框、内外都有边框

 

osdba-# \pset border 0 
Border style is 0. 
osdba-# \dp 
                     Access privileges 
Schema Name Type  Access privileges Column access privileges 
------ ---- ----- ----------------- ------------------------ 
public t    table                    
(1 row) 
 
osdba-# \pset border 1 
Border style is 1. 
osdba-# \dp 
                          Access privileges 
 Schema | Name | Type  | Access privileges | Column access privileges  
--------+------+-------+-------------------+-------------------------- 
 public | t    | table |                   |  
(1 row) 
 
osdba-# \pset border 2 
Border style is 2. 
osdba-# \dp 
                           Access privileges 
+--------+------+-------+-------------------+--------------------------+ 
| Schema | Name | Type  | Access privileges | Column access privileges | 
+--------+------+-------+-------------------+--------------------------+ 
| public | t    | table |                   |             | 
+--------+------+-------+-------------------+--------------------------+ 
(1 row)

2.\x命令-可以把表中的每一行的每列数据都拆分为单行展示,如果有一行数据有太多的拆行,显示不下,就可以使用这个命令

osdba-# \x 
Expanded display is on. 
osdba-# \dp 
Access privileges 
+-[ RECORD 1 ]-------------+--------+ 
| Schema                   | public | 
| Name                     | t      | 
| Type                     | table  | 
| Access privileges        |        | 
| Column access privileges |        | 
+--------------------------+--------+ 
 
osdba-# \x 
Expanded display is off. 
osdba-# \dp 
                           Access privileges 
+--------+------+-------+-------------------+--------------------------+ 
| Schema | Name | Type  | Access privileges | Column access privileges | 
+--------+------+-------+-------------------+--------------------------+ 
| public | t    | table |                   |         | 
+--------+------+-------+-------------------+--------------------------+ 
(1 row)

五、psql使用技巧

1.使用上下键把过去使用过的命令或者SQL语句调出来,连续两个tab键表示补全或给出提示输入

osdba-# \d 
\d    \dc   \dD   \dew  \dFd  \dg   \dn   \ds   \dT    
\da   \dC   \des  \df   \dFp  \di   \do   \dS   \du    
\db   \dd   \deu  \dF   \dFt  \dl   \dp   \dt   \dv

2.psql加上-E参数,可以把psql中各种以"\"开头的命令执行的实际SQL打印出来

-bash-4.1$ psql -E postgres 
psql (8.4.20) 
Type "help" for help. 
 
postgres=# \d 
********* QUERY ********** 
SELECT n.nspname as "Schema", 
  c.relname as "Name", 
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' 
  THEN 'sequence'WHEN 's' THEN 'special' END as "Type", 
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner" 
FROM pg_catalog.pg_class c 
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace 
WHERE c.relkind IN ('r','v','S','') 
      AND n.nspname  'pg_catalog' 
      AND n.nspname  'information_schema' 
      AND n.nspname !~ '^pg_toast' 
  AND pg_catalog.pg_table_is_visible(c.oid) 
ORDER BY 1,2; 
************************** 
 
No relations found

如果你在使用之后,想立即关闭

postgres=# \set ECHO_HIDDEN off 
postgres=# \d 
No relations found.

以上就是postgresql快速入门:psql工具的使用_mysql的内容,更多相关内容请关注php中文网(www.php.cn)!

相关文章

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

postgresql mysql

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

2026.11.17

0

15

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.17

0

15

GoFrame框架零基础入门
GoFrame框架零基础入门

本专题整理GoFrame框架的入门知识,从模块化设计理念、环境准备到跑通第一个Web服务,讲清目录分层、配置加载、日志与中间件的用法,并对比Gin、Go-Zero的差异,帮助Go开发者建立整体认知。

2026.11.17

0

15

SQLAlchemy 一对多关系怎么建立
SQLAlchemy 一对多关系怎么建立

SQLAlchemy 一对多关系怎么建立?本专题整理父子表外键、relationship 双向映射、关联查询、级联删除和加载优化等相关文章,帮助你理解并实现常见的 ORM 关系建模。

2026.11.17

0

10

SQLAlchemy 如何增删改查
SQLAlchemy 如何增删改查

SQLAlchemy 如何增删改查?本专题整理 ORM 新增记录、条件查询、更新和删除、Session 事务提交与批量处理等实用文章,帮助你掌握 Python 数据库操作流程。

2026.11.17

0

11

SQLAlchemy ORM 模型怎么写
SQLAlchemy ORM 模型怎么写

SQLAlchemy ORM 模型怎么写?本专题整理模型类定义、Mapped 字段映射、主键与约束、表结构创建和常见报错的相关文章,帮助你从基础配置到实际建模逐步掌握 SQLAlchemy。

2026.11.17

0

16

Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

160

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

120

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

100

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL vs MySQL
PostgreSQL vs MySQL

共1课时 | 198人学习

PostgreSQL 教程
PostgreSQL 教程

共48课时 | 21万人学习

PostgreSQL 手册
PostgreSQL 手册

共0课时 | 1万人学习