psql.1
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 1 代码 · 共 1,822 行 · 第 1/4 页
1
1,822 行
\fBPORT\fRThe database server port to which you are currently connected.This is set every time you connect to a database (includingprogram start-up), but can be unset..TP\fBPROMPT1\fR.TP\fBPROMPT2\fR.TP\fBPROMPT3\fRThese specify what the prompts \fBpsql\fRissues should look like. See Prompting [\fBpsql\fR(1)] below..TP\fBQUIET\fRThis variable is equivalent to the command line option\fB-q\fR. It is probably not too useful ininteractive mode..TP\fBSINGLELINE\fRThis variable is equivalent to the command line option\fB-S\fR..TP\fBSINGLESTEP\fRThis variable is equivalent to the command line option\fB-s\fR..TP\fBUSER\fRThe database user you are currently connected as. This is setevery time you connect to a database (including programstart-up), but can be unset..TP\fBVERBOSITY\fRThis variable can be set to the values default,verbose, or terse to control the verbosityof error reports..SS "SQL INTERPOLATION".PPAn additional useful feature of \fBpsql\fRvariables is that you can substitute (``interpolate'')them into regular SQL statements. The syntax forthis is again to prepend the variable name with a colon(:)..sp.nftestdb=> \fB\\set foo 'my_table'\fRtestdb=> \fBSELECT * FROM :foo;\fR.sp.fiwould then query the table my_table. The value ofthe variable is copied literally, so it can even contain unbalancedquotes or backslash commands. You must make sure that it makes sensewhere you put it. Variable interpolation will not be performed intoquoted SQL entities..PPA popular application of this facility is to refer to the lastinserted OID in subsequent statements to build aforeign key scenario. Another possible use of this mechanism is tocopy the contents of a file into a table column. First load the file into avariable and then proceed as above..sp.nftestdb=> \fB\\set content '''' `cat my_file.txt` ''''\fRtestdb=> \fBINSERT INTO my_table VALUES (:content);\fR.sp.fiOne problem with this approach is that \fImy_file.txt\fRmight contain single quotes. These need to be escaped so thatthey don't cause a syntax error when the second line is processed. Thiscould be done with the program \fBsed\fR:.sp.nftestdb=> \fB\\set content '''' `sed -e "s/'/''/g" < my_file.txt` ''''\fR.sp.fiIf you are using non-standard-conforming strings then you'll also needto double backslashes. This is a bit tricky:.sp.nftestdb=> \fB\\set content '''' `sed -e "s/'/''/g" -e 's/\\\\/\\\\\\\\/g' < my_file.txt` ''''\fR.sp.fiNote the use of different shell quoting conventions so that neitherthe single quote marks nor the backslashes are special to the shell.Backslashes are still special to \fBsed\fR, however, sowe need to double them. (Perhapsat one point you thought it was great that all Unix commands use thesame escape character.).PPSince colons may legally appear in SQL commands, the following ruleapplies: the character sequence``:name'' is not changed unless ``name'' is the nameof a variable that is currently set. In any case you can escapea colon with a backslash to protect it from substitution. (Thecolon syntax for variables is standard SQL forembedded query languages, such as \fBECPG\fR.The colon syntax for array slices and type casts arePostgreSQL extensions, hence theconflict.).SS "PROMPTING".PPThe prompts \fBpsql\fR issues can be customizedto your preference. The three variables PROMPT1,PROMPT2, and PROMPT3 contain stringsand special escape sequences that describe the appearance of theprompt. Prompt 1 is the normal prompt that is issued when\fBpsql\fR requests a new command. Prompt 2 isissued when more input is expected during command input because thecommand was not terminated with a semicolon or a quote was not closed.Prompt 3 is issued when you run an SQL\fBCOPY\fR command and you are expected to type in therow values on the terminal..PPThe value of the selected prompt variable is printed literally,except where a percent sign (%) is encountered.Depending on the next character, certain other text is substitutedinstead. Defined substitutions are:.TP\fB%M\fRThe full host name (with domain name) of the database server,or [local] if the connection is over a Unixdomain socket, or[local:\fI/dir/name\fR], if the Unix domain socket is not at the compiled in defaultlocation..TP\fB%m\fRThe host name of the database server, truncated at thefirst dot, or [local] if the connection isover a Unix domain socket..TP\fB%>\fRThe port number at which the database server is listening..TP\fB%n\fRThe database session user name. (The expansion of thisvalue might change during a database session as the resultof the command \fBSET SESSIONAUTHORIZATION\fR.).TP\fB%/\fRThe name of the current database..TP\fB%~\fRLike %/, but the output is ~(tilde) if the database is your default database..TP\fB%#\fRIf the session user is a database superuser, then a#, otherwise a >.(The expansion of this value might change during a databasesession as the result of the command \fBSET SESSIONAUTHORIZATION\fR.).TP\fB%R\fRIn prompt 1 normally =, but ^ ifin single-line mode, and ! if the session isdisconnected from the database (which can happen if\fB\\connect\fR fails). In prompt 2 the sequence isreplaced by -, *, a single quote,a double quote, or a dollar sign, depending on whether\fBpsql\fR expects more input because thecommand wasn't terminated yet, because you are inside a/* ... */ comment, or because you are insidea quoted or dollar-escaped string. In prompt 3 the sequence doesn'tproduce anything..TP\fB%x\fRTransaction status: an empty string when not in a transactionblock, or * when in a transaction block, or! when in a failed transaction block, or ?when the transaction state is indeterminate (for example, becausethere is no connection)..TP\fB%\fIdigits\fB\fRThe character with the indicated octal code is substituted..TP\fB%:\fIname\fB:\fRThe value of the \fBpsql\fR variable\fIname\fR. See thesection Variables [\fBpsql\fR(1)] for details..TP\fB%`\fIcommand\fB`\fRThe output of \fIcommand\fR, similar to ordinary``back-tick'' substitution..TP\fB%[ ... %]\fRPrompts may contain terminal control characters which, forexample, change the color, background, or style of the prompttext, or change the title of the terminal window. In order forthe line editing features of \fBReadline\fR to work properly, thesenon-printing control characters must be designated as invisibleby surrounding them with %[ and%]. Multiple pairs of these may occur withinthe prompt. For example,.sp.nftestdb=> \\set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '.sp.firesults in a boldfaced (1;) yellow-on-black(33;40) prompt on VT100-compatible, color-capableterminals..PPTo insert a percent sign into your prompt, write%%. The default prompts are\&'%/%R%# ' for prompts 1 and 2, and\&'>> ' for prompt 3..sp.RS.B "Note:"This feature was shamelessly plagiarized from\fBtcsh\fR..RE.sp.SS "COMMAND-LINE EDITING".PP\fBpsql\fR supports the \fBReadline\fRlibrary for convenient line editing and retrieval. The commandhistory is automatically saved when \fBpsql\fRexits and is reloaded when\fBpsql\fR starts up. Tab-completion is alsosupported, although the completion logic makes no claim to be anSQL parser. If for some reason you do not like the tab completion, youcan turn it off by putting this in a file named\fI\&.inputrc\fR in your home directory:.sp.nf$if psqlset disable-completion on$endif.sp.fi(This is not a \fBpsql\fR but a\fBReadline\fR feature. Read its documentationfor further details.).SH "ENVIRONMENT".TP\fBPAGER\fRIf the query results do not fit on the screen, they are pipedthrough this command. Typical values aremore or less. The defaultis platform-dependent. The use of the pager can be disabled byusing the \fB\\pset\fR command..TP\fBPGDATABASE\fRDefault connection database.TP\fBPGHOST\fR.TP\fBPGPORT\fR.TP\fBPGUSER\fRDefault connection parameters.TP\fBPSQL_EDITOR\fR.TP\fBEDITOR\fR.TP\fBVISUAL\fREditor used by the \fB\\e\fR command. The variablesare examined in the order listed; the first that is set is used..TP\fBSHELL\fRCommand executed by the \fB\\!\fR command..TP\fBTMPDIR\fRDirectory for storing temporary files. The default is\fI/tmp\fR..PPThis utility, like most other PostgreSQL utilities,also uses the environment variables supported by \fBlibpq\fR(see in the documentation)..PP.SH "FILES".TP 0.2i\(buBefore starting up, \fBpsql\fR attempts toread and execute commands from the system-wide\fIpsqlrc\fR file and the user's\fI~/.psqlrc\fR file.(On Windows, the user's startup file is named\fI%APPDATA%\\postgresql\\psqlrc.conf\fR.)See \fIPREFIX/share/psqlrc.sample\fRfor information on setting up the system-wide file. It could be usedto set up the client or the server to taste (using the \fB\\set\fRand \fBSET\fR commands)..TP 0.2i\(buBoth the system-wide \fIpsqlrc\fR file and the user's\fI~/.psqlrc\fR file can be made version-specificby appending a dash and the PostgreSQLrelease number, for example \fI~/.psqlrc-8.2.6\fR.A matching version-specific file will be read in preference to anon-version-specific file..TP 0.2i\(buThe command-line history is stored in the file\fI~/.psql_history\fR, or\fI%APPDATA%\\postgresql\\psql_history\fR on Windows..SH "NOTES".TP 0.2i\(buIn an earlier life \fBpsql\fR allowed thefirst argument of a single-letter backslash command to startdirectly after the command, without intervening whitespace. Forcompatibility this is still supported to some extent,but we are not going to explain the details here as this use isdiscouraged. If you get strange messages, keep this in mind.For example.sp.nftestdb=> \fB\\foo\fRField separator is "oo"..sp.fiwhich is perhaps not what one would expect..TP 0.2i\(bu\fBpsql\fR only works smoothly with serversof the same version. That does not mean other combinations willfail outright, but subtle and not-so-subtle problems might comeup. Backslash commands are particularly likely to fail if theserver is of a different version..SH "NOTES FOR WINDOWS USERS".PP\fBpsql\fR is built as a ``consoleapplication''. Since the Windows console windows use a differentencoding than the rest of the system, you must take special carewhen using 8-bit characters within \fBpsql\fR.If \fBpsql\fR detects a problematicconsole code page, it will warn you at startup. To change theconsole code page, two things are necessary:.TP 0.2i\(buSet the code page by entering \fBcmd.exe /c chcp1252\fR. (1252 is a code page that is appropriate forGerman; replace it with your value.) If you are using Cygwin,you can put this command in \fI/etc/profile\fR..TP 0.2i\(buSet the console font to Lucida Console, because theraster font does not work with the ANSI code page..PP.SH "EXAMPLES".PPThe first example shows how to spread a command over several lines ofinput. Notice the changing prompt:.sp.nftestdb=> \fBCREATE TABLE my_table (\fRtestdb(> \fB first integer not null default 0,\fRtestdb(> \fB second text)\fRtestdb-> \fB;\fRCREATE TABLE.sp.fiNow look at the table definition again:.sp.nftestdb=> \fB\\d my_table\fR Table "my_table" Attribute | Type | Modifier-----------+---------+-------------------- first | integer | not null default 0 second | text |.sp.fiNow we change the prompt to something more interesting:.sp.nftestdb=> \fB\\set PROMPT1 '%n@%m %~%R%# '\fRpeter@localhost testdb=>.sp.fiLet's assume you have filled the table with data and want to take alook at it:.sp.nfpeter@localhost testdb=> SELECT * FROM my_table; first | second-------+-------- 1 | one 2 | two 3 | three 4 | four(4 rows).sp.fiYou can display tables in different ways by using the\fB\\pset\fR command:.sp.nfpeter@localhost testdb=> \fB\\pset border 2\fRBorder style is 2.peter@localhost testdb=> \fBSELECT * FROM my_table;\fR+-------+--------+| first | second |+-------+--------+| 1 | one || 2 | two || 3 | three || 4 | four |+-------+--------+(4 rows)peter@localhost testdb=> \fB\\pset border 0\fRBorder style is 0.peter@localhost testdb=> \fBSELECT * FROM my_table;\fRfirst second----- ------ 1 one 2 two 3 three 4 four(4 rows)peter@localhost testdb=> \fB\\pset border 1\fRBorder style is 1.peter@localhost testdb=> \fB\\pset format unaligned\fROutput format is unaligned.peter@localhost testdb=> \fB\\pset fieldsep ","\fRField separator is ",".peter@localhost testdb=> \fB\\pset tuples_only\fRShowing only tuples.peter@localhost testdb=> \fBSELECT second, first FROM my_table;\fRone,1two,2three,3four,4.sp.fiAlternatively, use the short commands:.sp.nfpeter@localhost testdb=> \fB\\a \\t \\x\fROutput format is aligned.Tuples only is off.Expanded display is on.peter@localhost testdb=> \fBSELECT * FROM my_table;\fR-[ RECORD 1 ]-first | 1second | one-[ RECORD 2 ]-first | 2second | two-[ RECORD 3 ]-first | 3second | three-[ RECORD 4 ]-first | 4second | four.sp.fi
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?