psql使用

来源:互联网 发布:tl494引脚功能和数据 编辑:程序博客网 时间:2024/04/27 21:37

psql运行的时候读取一个叫psqlrc的配置文件。当psql启动时候,它会查找这个文件运行文件中的命令来初始化环境。

在unix系统上文件叫.psqlrc在home目录下。在windows上该文件叫psqlrc.conf,在%APP-DATA%\postgresql目录下。通常是c:\User\your_login\AppData\Roaming\postgresql

通常该文件是没有的,需要你手工去创建,该文件中任何的设置都会覆盖psql默认值。

如果在启动psql的时候不想检查psqlrc,使用-X参数。

连接到数据库

psql –h hostname –p 5432 –d dbname –U username –W

查看有哪些数据库

psql -l或

\l

显示所有的schema

\dn

显示所有的表空间

\db

显示所有的角色和用户

\dg

\du

查看表的分配权限

\dp

或\z

设置客户端的字符编码

\encoding gbk

\encoding utf8

把每行的每列数据展示成1行,类似于mysql的\G

\x

显示一些提示说明信息

\echo hello world


执行sql文件

psql –f examples.sql

使用psql执行sql命令

psql -c "select current_time" -U postgres

查看命令执行的时间

\timing on

默认情况下的自动提交时启用的,要设置自动提交关闭

\set autocommit off

设置快捷键

postgres=# \set tt 'select current_time';
postgres=# :tt
     timetz
-----------------
 17:04:13.883+08
(1 行记录)
时间:0.692 ms
释放一个变量

\unset tt

获取之前的命令

使用上箭头键可以查看之前执行的命令,用下面的方法设置历史命令的条数

\set histsize 10

执行操作系统命令

\! ipconfig

切换数据库

\c dbname

查看所有表

postgres=# \d
              关联列表
 架构模式 | 名称 |  型别  |  拥有者
----------+------+--------+----------
 public   | t    | 资料表 | postgres
(1 行记录)

更详细的信息

postgres=# \dt+
                       关联列表
 架构模式 | 名称 |  型别  |  拥有者  |  大小   | 描述
----------+------+--------+----------+---------+------
 public   | t    | 资料表 | postgres | 0 bytes |
(1 行记录)

查看表的具体信息
postgres=# \d t
    资料表 "public.t"
 栏位 |  型别   | 修饰词
------+---------+--------
 id   | integer |

更详细的信息

postgres=# \d+ t
                 资料表 "public.t"
 栏位 |  型别   | 修饰词 | 存储  | 统计目标 | 描述
------+---------+--------+-------+----------+------
 id   | integer |        | plain |          |
有 OIDs: 否

得到psql中命令实际执行的sql,有时候想看下\d 实际执行的sql是什么,可以使用-E参数

psql -E postgres

要是在一个已经打开的psql中看实际执行sql,那么可以使用\set ECHO_HIDDEN on|off临时的打开在关闭


查看sql语句报告

将下面的语句放入到setting_report.psql文件中

\o settings_report.html 
\T 'cellspacing=0 cellpadding=0' 
\qecho '<html><head><style>H2{color:maroon}</style>' 
\qecho '<title>PostgreSQL Settings</title></head><body>' 
\qecho '<table><tr valign=''top''><td><h2>Planner Settings</h2>' 
\x on 
\t on 
\pset format html 
SELECT category, string_agg(name || '=' || setting, E'\n' ORDER BY name              ) As settings 
FROM pg_settings 
WHERE category LIKE '%Planner%' 
GROUP BY category 
ORDER BY category; 
\H 
\qecho '</td><td><h2>File Locations</h2>' 
\x off 
\t on 
\pset format html 
SELECT name, setting FROM pg_settings WHERE category = 'File Locations' ORDER BY name; 
\qecho '<h2>Memory Settings</h2>' 
SELECT name, setting, unit FROM pg_settings WHERE category ILIKE '%memory%' ORDER BY name; 
\qecho '</td></tr></table>' 
\qecho '</body></html>' 
\o 

运行psql -U postgres -f D:\settings_report.psql,我们在PostgreSQL的安装目录下发现有个setting_report.html的文件生成,该文件中包含了优化器的一些设置和参数设置。

默认psql在执行文件的时候,遇到错误会继续执行,如果想要遇到第一个错误后就停止,使用下面的方式

psql -f test.sql -v ON_ERROR_STOP=on

dellstore2=# \t
关闭只显示元组。
dellstore2=# \o D:\test.sql
D:: Permission denied
dellstore2=# \o D:\\test.sql
D:: Permission denied
dellstore2=# \o test.sql 输出到文件
dellstore2=# select 'alter table'||n.nspname||'.'||c.relname||'add column last_u
pdate_timestamp timestamp with time zone;' from pg_class c join pg_namespace n o
n c.relnamespace = n.oid where n.nspname='test';

dellstore2=# \! cat test.sql 调用操作系统命令查看文件
                                  ?column?
-----------------------------------------------------------------------------
 alter tabletest.aadd column last_update_timestamp timestamp with time zone;
 alter tabletest.badd column last_update_timestamp timestamp with time zone;
 alter tabletest.cadd column last_update_timestamp timestamp with time zone;
(3 ÐмǼ)


dellstore2=# \i test.sql 执行sql文件
psql:test.sql:3: ERROR:  syntax error at or near "?"
第1行?column?
     ^
psql:test.sql:4: ERROR:  syntax error at or near "tabletest"
第1行alter tabletest.badd column last_update_timestamp timestamp ...
           ^
psql:test.sql:5: ERROR:  syntax error at or near "tabletest"
第1行alter tabletest.cadd column last_update_timestamp timestamp ...
           ^
psql:test.sql:7: ERROR:  syntax error at or near "3"
第1行(3 行记录)
      ^
dellstore2=#



psql的参数在下面的链接中

http://blog.csdn.net/aoerqileng/article/details/47205577

0 0
原创粉丝点击