PostgreSQL命令行psql常用命令

1,364 阅读1分钟

1、登录数据库

psql -h 127.0.0.1 -p 5432 -d database -U postgres

-h 数据库ip -p 端口号 -d 数据库名 -U 登录用户名

2、 导入SQL脚本 示例:

psql -U postgres -d database -f sqlScript.sql

将sqlScript.sql导入到名为database的数据库中

常用命令 3、展示数据库

\l 或者 \list 

支持正则匹配,例如展示包含post字符的数据库

\l '*post*'

4、切换数据库(创建新的数据库连接) \c 可选参数 dbname [ username ] [ host ] [ port ] eg:

\c postgres
或者
\c postgres username localhost 5432

5、展示当前数据库下所有关系(table、view、sequence等)

\d 

展示当前所有表

\d “Account” 展示Account表字段信息 

展示当前数据库下所有schema信息

\dn 

显示当前使用的schema

SHOW search_path; 

切换当前schema

SET search_path TO myschema;
set search_path to auth;

SHOW search_path;

断开数据库连接

\q