PostgreSQL 与 MySQL 常用命令对照

有很多 MySQL 的用户在使用 PostgreSQL 时,对于 PostgreSQL 客户端(psql)的常用命令不是很熟悉,有时甚至会感到困惑,在此罗列 MySQL 客户端 mysql 与 PostgreSQL 客户端 psql 常用命令的对照关系,方便大家参考。

在此之前,先来看看帮助命令,二者均可以通过 \? 命令查看帮助,获取其支持的命令列表,详情可参考对应官网

mysql psql 说明
q (exit/quit) q 退出命令,PostgreSQL 11 也已经支持 quitexit
select version() select version() 版本信息
select now() select now() 当前时间
select current_user select current_user 当前用户
select database() select current_database() 当前 database
show databases l 显示所有 database
show tables dt 也可以使用\d,显示所有表,视图和序列
show processlist select * from pg_stat_activity 活跃进程/线程列表
show create table tablename no equivalent psql 中没有等价的命令,可以使用 pg_dump --schema-only -t tablename 查看建表语句
show index from tablename d tablename \d tablename会显示索引,触发器,规则和约束
show triggers from tablename d tablename 同上
desc tablename d tablename 同上
no equivalent dn 显示所有 schema,MySQL 没有这个概念
u dbname (use dbname) c dbname 切换 database
G x 查询结果每列显示一行,\G 跟在每个查询后边
总是显示执行时间 timing 显示 SQL 执行时间
C charset encoding encoding 设置字符集,\encoding 可以显示当前字符集
r [dbname] [dbhost] c [dbname] [dbhost] 重连 server
h [command] h [command] help
n pset pager off 关闭 pager,打开 pager 时会用 less 展示结果
P pset pager always 打开 pager,用 less 展示结果
\. file.sql(source file.sql) i file.sql 从外部文件读取 SQL 执行

以上简单罗列 MySQL 和 PostgreSQL 常用命令的对照关系,欢迎大家补充。

Reference

  1. psql https://www.postgresql.org/docs/11/static/app-psql.html
  2. mysql https://dev.mysql.com/doc/refman/8.0/en/mysql.html
  3. https://www.endpoint.com/blog/2009/12/24/mysql-and-postgres-command-equivalents

猜你喜欢

转载自yq.aliyun.com/articles/672033