psql功能强大,技巧怎么用只能后续慢慢补充了,看一个工具先看怎么用help
$ psql #连接到数据库服务器,可以在其中输入相应的SQL语句或者psql的命令,psql的命令都以\开始
$ \l #列出所有的数据库
$ \d #查看表的结构
$ \d 表名 #查看指定表的具体结构
$ create database 新数据库名 #创建新的数据库
$ \c 已经有的数据库名 #切换当前数据库到指定的数据库
$ psql -h 主机名/ip地址 -p 端口号 数据库名称 用户名称 #连接到指定的数据库
$ \d #显示所有的表
$ \d 表名 #显示指定的表的结构
$ \d 索引名 #显示指定索引的结构
$ \d 后跟通配符或者?等 #?代表匹配一个 代表匹配多个,如\d s*会匹配以s开头后跟任意个字符的表或者索引并显示出来,\d s?会匹配以s开头后跟一个字符的表或者索引并显示出来
$ \d+ #该命令会比\d命令显示更详细的信息,除了前面介绍的那些,它还会显示任何与表相关联的注释,以及表中出现的OID,\d+可以代替之前的任意以\d开头的命令
$ \dt #只显示表
$ \di #只显示所有的索引
$ \ds #只显示序列
$ \dv #只显示视图
$ \df #只显示函数
$ \timing on #显示SQL的执行时间
$ \timing off #关闭显示SQL的执行时间
$ \dn #显示所有的schema
$ \db #显示所有的表空间
$ \du 或者 \dg #显示数据库中的所有角色和用户
$ \dp 或者 \z 表名 #显示表的权限分配情况
$ \encoding utf8 #当客户端的字符编码和服务器的不一样时,可能会显示乱码,可以使用\encoding命令来指定客户端的字符编码,如使用\encoding utf8来指定客户端的编码方式为utf8
\pset命令用于指定输出的格式,具体如下:
\pset border 0 : 表示输出内容物边框
\pset border 1 : 表示边框只在内部,默认情况下采用的是该条命令
\pset border 2 : 表示内外都存在边框
历史命令与补全功能
使用上下键可以查看历史命令
两次tab键可以实现命令补全或者给出输入提示
在psql中事务是自动提交的,比方说,执行完一条create table语句之后,事务就会被自动的提交,如果不想自动的提交,方法有两种:
方案一:运行begin命令,然后执行相应的SQL语句,最后在执行commit或者rollback语句
方案二:直接使用psql中的命令关闭自动提交的功能,\set AUTOCOMMIT off ,该命令设置自动提交关闭
实际上,MySQL等数据库也是这样的情况,当我们不使用显示的事务语句时,每一条SQL语句都被作为一个事务,或者说数据库内部将每一个SQL语句封装为一个事务,在PostgreSQL中仍然是这样的,平时使用时要注意。
得到psql中真实执行的SQL语句
aoldbs=# help
You are using psql, the command-line interface to PostgreSQL.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
aoldbs=#
eg:查看SQL commands
aoldbs=# \h drop table
Command: DROP TABLE
Description: remove a table
Syntax:
DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
aoldbs=#
\?查看PSQL命令
General
\copyright show PostgreSQL usage and distribution terms
\g [FILE] or ; execute query (and send results to file or |pipe)
\gset [PREFIX] execute query and store results in psql variables
\q quit psql
\watch [SEC] execute query every SEC seconds
Help
\? [commands] show help on backslash commands
\? options show help on psql command-line options
\? variables show help on special variables
\h [NAME] help on syntax of SQL commands, * for all commands
Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\w FILE write query buffer to file
Input/Output
\copy ... perform SQL COPY with data stream to the client host
\echo [STRING] write string to standard output
\i FILE execute commands from file
\ir FILE as \i, but relative to location of current script
\o [FILE] send all query results to file or |pipe
\qecho [STRING] write string to query output stream (see \o)
Informational
(options: S = show system objects, + = additional detail)
\d[S+] list tables, views, and sequences
\d[S+] NAME describe table, view, sequence, or index
\da[S] [PATTERN] list aggregates
\db[+] [PATTERN] list tablespaces
\dc[S+] [PATTERN] list conversions
\dC[+] [PATTERN] list casts
\dd[S] [PATTERN] show object descriptions not displayed elsewhere
\ddp [PATTERN] list default privileges
\dD[S+] [PATTERN] list domains
\det[+] [PATTERN] list foreign tables
\des[+] [PATTERN] list foreign servers
\deu[+] [PATTERN] list user mappings
\dew[+] [PATTERN] list foreign-data wrappers
\df[antw][S+] [PATRN] list [only agg/normal/trigger/window] functions
\dF[+] [PATTERN] list text search configurations
\dFd[+] [PATTERN] list text search dictionaries
\dFp[+] [PATTERN] list text search parsers
\dFt[+] [PATTERN] list text search templates
\dg[+] [PATTERN] list roles
\di[S+] [PATTERN] list indexes
\dl list large objects, same as \lo_list
\dL[S+] [PATTERN] list procedural languages
\dm[S+] [PATTERN] list materialized views
\dn[S+] [PATTERN] list schemas
\do[S] [PATTERN] list operators
\dO[S+] [PATTERN] list collations
\dp [PATTERN] list table, view, and sequence access privileges
\drds [PATRN1 [PATRN2]] list per-database role settings
\ds[S+] [PATTERN] list sequences
\dt[S+] [PATTERN] list tables
\dT[S+] [PATTERN] list data types
\du[+] [PATTERN] list roles
\dv[S+] [PATTERN] list views
\dE[S+] [PATTERN] list foreign tables
\dx[+] [PATTERN] list extensions
\dy [PATTERN] list event triggers
\l[+] [PATTERN] list databases
\sf[+] FUNCNAME show a function‘s definition
\z [PATTERN] same as \dp
Formatting
\a toggle between unaligned and aligned output mode
\C [STRING] set table title, or unset if none
\f [STRING] show or set field separator for unaligned query output
\H toggle HTML output mode (currently off)
\pset [NAME [VALUE]] set table output option
(NAME := {format|border|expanded|fieldsep|fieldsep_zero|footer|null|
numericlocale|recordsep|recordsep_zero|tuples_only|title|tableattr|pager|
unicode_border_linestyle|unicode_column_linestyle|unicode_header_linestyle})
\t [on|off] show only rows (currently off)
\T [STRING] set HTML <table> tag attributes, or unset if none
\x [on|off|auto] toggle expanded output (currently off)
Connection
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
connect to new database (currently "aoldbs")
\encoding [ENCODING] show or set client encoding
\password [USERNAME] securely change the password for a user
\conninfo display information about current connection
Operating System
\cd [DIR] change the current working directory
\setenv NAME [VALUE] set or unset environment variable
\timing [on|off] toggle timing of commands (currently off)
! [COMMAND] execute command in shell or start interactive shell
Variables
\prompt [TEXT] NAME prompt user to set internal variable
\set [NAME [VALUE]] set internal variable, or list all if no parameters
\unset NAME unset (delete) internal variable
Large Objects
\lo_export LOBOID FILE
\lo_import FILE [COMMENT]
\lo_list
\lo_unlink LOBOID large object operations
原文地址:http://blog.51cto.com/aklaus/2059098