postgresql常用命令

发布于:2023-09-14 ⋅ 阅读:(78) ⋅ 点赞:(0)

\? #打印出所有跟\相关的命令

\h #打印出所有跟sql相关的命令

\h sql #查看具体sql相关的帮助,例如\h create user 是查看跟create user相关的帮助

\l #列出所有的数据库信息

\c #databasename #连接进入某个数据库

\d #查看当前数据库中所有表、视图、序列

\db #查看表空间的信息

\dn #查看所有的模式信息

\dg或者\du #查看数据库中所有的角色或用户

\dt #列出所有的表信息

\d tablename #查看某个表的结构

\du #查询数据库的用户信息

\x #设置查询结果的输出模式 (\x 竖行显示)

以反斜线开头的命令被称为元命令,如\db等。使用技巧如下:

1.获取元命令对一个的sql代码

需要在psql连接数据库时增加“-E”选项

2. 自动补全功能

使用tab键可以自动补全,例如要查询表department(select * from department),在输入“d”之后按tab键会自动出现department,再次按tab键,则会列出所有以d开头的表对象。

3.显示sql语句执行时间

使用"\timing"命令,可以打开显示sql语句执行时间的开关。可以再次使用一次"\timing"命令,则关掉该开关。

postgres=# \h

Available help:

ABORT CREATE FOREIGN DATA WRAPPER DROP ROUTINE

ALTER AGGREGATE CREATE FOREIGN TABLE DROP RULE

ALTER COLLATION CREATE FUNCTION DROP SCHEMA

ALTER CONVERSION CREATE GROUP DROP SEQUENCE

postgres=# \h alter AGGREGATE

Command: ALTER AGGREGATE

Description: change the definition of an aggregate function

Syntax:

ALTER AGGREGATE name ( aggregate_signature ) RENAME TO new_name

ALTER AGGREGATE name ( aggregate_signature )

OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }

ALTER AGGREGATE name ( aggregate_signature ) SET SCHEMA new_schema

where aggregate_signature is:

postgres=# select * from pg_tables; --查看所有的表信息

schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity

--------------------+-------------------------+------------+------------+------------+----------+-------------+-------------

pg_catalog | pg_statistic | postgres | | t | f | f | f

pg_catalog | pg_type | postgres | | t | f | f | f

pg_catalog | pg_foreign_table | postgres | | t | f | f | f

pg_catalog | pg_authid | postgres | pg_global | t | f | f | f

postgres=# select * from pg_tables where schemaname!='pg_catalog';

schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity

--------------------+-------------------------+------------+------------+------------+----------+-------------+-------------

information_schema | sql_parts | postgres | | f | f | f | f

information_schema | sql_implementation_info | postgres | | f | f | f | f

information_schema | sql_features | postgres | | f | f | f | f

information_schema | sql_sizing | postgres | | f | f | f | f

(4 rows)

postgres=#


网站公告

今日签到

点亮在社区的每一天
去签到