psql.1 10.1 KB
Newer Older
1 2
.\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here....
3
.\" $Header: /cvsroot/pgsql/src/man/Attic/psql.1,v 1.15 1997/11/14 21:38:16 momjian Exp $
4
.TH PSQL UNIX 1/20/96 PostgreSQL PostgreSQL
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
.SH NAME
psql \(em run the interactive query front-end
.SH SYNOPSIS
.BR psql
[\c
.BR "-a"
authsvc
]
[\c
.BR "-A"
]
[\c
.BR "-c"
query
]
[\c
.BR "-d"
dbName]
[\c
.BR "-e"
]
[\c
.BR "-f"
filename]
[\c
B
Bruce Momjian 已提交
30 31 32
.BR "-F"
separator]
[\c
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
.BR "-h"
hostname]
[\c
.BR "-H"
]
[\c
.BR "-l"
port]
[\c
.BR "-n"
]
[\c
.BR "-o"
filename
]
[\c
.BR "-p"
port]
[\c
.BR "-q"
]
[\c
.BR "-s"
]
[\c
.BR "-S"
]
[\c
.BR "-t"
]
[\c
B
Bruce Momjian 已提交
64 65 66 67
.BR "-T"
table-options
]
[\c
B
Bruce Momjian 已提交
68 69 70
.BR "-u"
]
[\c
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
.BR "-x"
]
[dbname]
.in -5n
.SH DESCRIPTION
psql is a interactive query front-end to Postgres.  It enables you to
type in queries interactively, issue them to Postgres, and see the query
results.
.IR psql
can be used in a pipe sequence, and automatically detects when it
is not listening or talking to a real tty.
.IR psql
is designed to be an enhanced version of the older
.IR "monitor"
program.
.PP
.IR "psql"
is a frontend application, like any other.  Hence, a
.IR "postmaster"
process must be running on the database server host before
.IR "psql"
is executed.  In addition, the correct
.IR "postmaster"
port number must be specified
as described below.
.PP
The optional argument
.IR dbname
specifies the name of the database to be accessed.  This database must
already have been created.
.IR dbname
defaults to the value of the
.SM USER
environment variable or, if that's not set, to the Unix account name of the
current user.
.PP
.IR "psql"
understands the following command-line options:
.TP
.BR "-a" " system"
Specifies an authentication system
.IR "system"
(see
B
Bruce Momjian 已提交
114
.IR pgintro (1))
115 116 117 118 119
to use in connecting to the
.IR postmaster
process.  The default is site-specific.
.TP
.BR "-A"
120
Turn off fill justification when printing out table elements.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
.TP
.BR "-c" " query"
Specifies that
.IR "psql"
is to execute one query string,
.IR "query" ,
and then exit.  This is useful for shell scripts, typically in
conjunction with the
.BR -q ""
options.
.BR -c
option in shell scripts.
.TP
.BR "-d" " dbName"
Specifies the name of the database to connect to.
.TP
.BR "-e" " "
Echo the query sent to the backend
.TP
.BR "-f" " filename"
Use the file
.IR "filename"
as the source of queries instead of reading queries interactively.
.TP
B
Bruce Momjian 已提交
145 146 147 148 149 150
.BR "-F" " separator"
Use
.IR "separator"
as the field separator.
The default is "|".
.TP
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
.BR "-h" " hostname"
Specifies the hostname of the machine on which the
.IR postmaster
is running.  Defaults to the name of the local host, or the value of
the
.SM PGHOST
environment variable (if set).
.TP
.BR "-H"
Turns on
.SM HTML3.0
tabular output.
.TP
.BR "-l"
Lists all available databases
.TP
.BR "-n"
Do not use the readline library for input line editing and command history.
.TP
B
Bruce Momjian 已提交
170 171 172
.BR "-o" " filename"
Put all output into filename
.TP
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
.BR "-p" " port"
Specifies the Internet TCP port on which the
.IR postmaster
is listening for connections.  Defaults to 5432, or the value of the
.SM PGPORT
environment variable (if set).
.TP
.BR "-q"
Specifies that
.IR psql
should do its work quietly.  By default, it
prints welcome and exit messages and prompts for each query, and prints
out the number of rows returned from a query.
If this option is used, none of this happens. This is useful with the
.BR -c
option in shell scripts.
.TP
.BR "-s"
Run in single-step mode where the user at prompted for each query before
it is sent to the backend.
.TP
.BR "-S"
Run ins single-line mode where each query is terminated by a newline,
instead of a semicolon.
.TP
.BR "-t"
199
Turn off printing of column names.
200 201 202 203
This is useful with the
.BR -c
option in shell scripts.
.TP
B
Bruce Momjian 已提交
204 205 206 207 208 209 210 211
.BR "-T" " table-options"
Allows you to specify options to be placed within the <table ...> tag
for
.SM HTML3.0
tabular output. For example
.BR border
will give you tables with borders.
.TP
B
Bruce Momjian 已提交
212 213 214
.BR "-u"
Turns on username/password authentication.
.TP
215
.BR "-x"
216 217 218 219
Turns on extended row format mode. When enabled each row will have its column
names printed on the left with the column values printed on the right.
This is useful for rows which are otherwise too long to fit into
one screen line. HTML row output supports this mode also.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
.PP
You may set environment variables to avoid typing some of the above
options.  See the
.SM "ENVIRONMENT VARIABLES"
section below.
.SH "CONNECTING TO A DATABASE"
.IR psql
attempts to make a connection to the database at the hostname and
port number specified on the command line.   If the connection could not
be made for any reason (e.g. insufficient privileges, postmaster is not
running on the server, etc)
.IR psql
will return an error that says
.nf
Connection to database failed.
.fi
The reason for the connection failure is not provided.
.SH "ENTERING QUERIES"
In normal operation, psql provides a prompt with the name of the
database that psql is current connected to followed by the string "=>".
For example,
.nf
B
Bruce Momjian 已提交
242 243
Welcome to the POSTGRESQL interactive sql monitor:
  Please read the file COPYRIGHT for copyright terms of POSTGRESQL
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

   type \e? for help on slash commands
   type \eq to quit
   type \eg or terminate with semicolon to execute query
 You are currently connected to the database: testdb

testdb=>
.fi
.PP
At the prompt, the user may type in SQL queries.  Unless the -S option
is set, input lines are sent to the backend when a query-terminating
semicolon is reached.
.PP
Whenever a query is executed, psql also polls for asynchronous notification
events generated by
.IR listen (l)
and
.IR notify (l).
.PP
.SH "PSQL COMMANDS"
264 265 266 267 268 269 270 271 272 273 274 275
Anything you enter in psql that begins with an unquoted backslash is a psql
command.  Anything else is SQL and simply goes into the current query buffer
(and once you have at least one complete query, it gets automatically 
submitted to the backend).  Psql commands are also called slash commands.
.PP
The format of a psql command is the backslash, followed immediately by
a command verb, then any arguments.  The arguments are separated from the
command verb and each other by any number of white space characters.
.PP
With single character command verbs, you don't actually need to separate the
command verb from the argument with white space, for historical reasons.
You should anyway.
276
.IP "\ea"
277
Toggle field alignment when printing out table elements.
278 279
.IP "\eC \fIcaption\fR"
Set the HTML3.0 table caption.
280
.IP "\econnect \fIdbname\fR \fIusername\fR"
281
Establish a connection to a new database. The previous connection is closed.
282 283 284 285 286
.IP "\ecopy \fItable\fR {FROM | TO} \fIfilename\fR"
Perform a frontend copy.  This is an operation that runs a SQL COPY command,
but instead of the backend reading or writing a specified file, and 
consequently requiring special user privilege, psql reads or writes the 
file and routes the data to or from the backend.
287 288 289 290 291 292 293 294
.IP "\ed [\fItable\fR]"
List tables in the database, or if
.IR table
is specified, list the columns in
.IR table.
If table name is
.IR *,
list all tables and column information for each tables.
295 296
.IP "\eda"
List aggregates.
B
Bruce Momjian 已提交
297
.IP "\edd object"
298
List the description of the table, table.column, type, operator, or aggregate.
B
Bruce Momjian 已提交
299 300
.IP "\edi"
List only indexes.
301 302
.IP "\edo"
List operators.
303 304
.IP "\eds"
List only sequences.
305 306 307 308
.IP "\edt"
List only tables.
.IP "\edT"
List types.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
.IP "\ee [\fIfilename\fR]"
Edit the current query buffer or \fIfile\fR.
.IP "\eE [\fIfilename\fR]"
Edit the current query buffer or \fIfile\fR and execute it
upon editor exit.
.IP "\ef [\fIseparator\fR]"
Set the field separator.  Default is a single blank space.
.IP "\eg [\fI|command\fR] | [\fIfilename\fR]"
Send the current query input buffer to the backend and optionally
save the output in
.IR filename
or pipe the output into
.IR "|command".
.IP "\eh [\fIcommand\fR]"
Give syntax help on the specified SQL command.  If the
.IR command
is not specified, list all the commands for which syntax help is
available.  If the
.IR command
is
.IR *,
give syntax help on all SQL commands.
.IP "\eH"
Toggle html3 output.
.IP "\ei \fIfilename\fR"
Read queries from
.IR filename
into the query input buffer.
.IP "\el"
List all the databases in the server.
.IP "\em"
Toggle monitor-like table display.
This is standard SQL output (i.e extra border characters).
.IP "\eo [\fI|command\fR] | [\fIfilename\fR]"
Send query results to
.IR filename .
Or pipe into
.IR command .
If no arguments are specified, send query results to
.IR stdout .
.IP "\ep"
Print the current query buffer.
.IP \eq
Quit the psql program.
.IP "\er"
Reset(clear) the query buffer.
.IP "\es [\fIfilename\fR]"
Print or save the command line history to \fIfilename\fR.  (Only available if psql is
configured to use readline)
.IP "\et"
359
Toggle display of output column name headings and row count (defaults to on).
360 361 362
.IP "\eT"
Set html3.0 <table ...> options.
.IP "\ex"
363 364 365 366
Toggles extended row format mode. When enabled each row will have its column
names printed on the left with the column values printed on the right.
This is useful for rows which are otherwise too long to fit into
one screen line. HTML row output mode supports this flag too.
367 368 369
.IP "\ez"
Produces a list of all tables in database with their appropriate ACLs
(grant/revoke permissions) listed.
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
.IP "\e! [\fIcommand\fR]"
Escape to shell or execute
.IR command.
.IP \e?
Get help information about the \e commands.
.SH "ENVIRONMENT VARIABLES"
You may set any of the following environment variables to avoid
specifying command-line options:
.nf
hostname:	PGHOST
port:		PGPORT
tty:		PGTTY
options:		PGOPTION
realm:		PGREALM
.fi
.PP
If
.SM PGOPTION
is specified, then the options it contains are parsed
.BR before
any command-line options.
.PP
.SM PGREALM
only applies if
.IR Kerberos
authentication is in use.  If this environment variable is set, Postgres
will attempt authentication with servers for this realm and use
separate ticket files to avoid conflicts with local ticket files.  See
B
Bruce Momjian 已提交
398
.IR pgintro (1)
399 400 401 402
for additional information on
.IR Kerberos .
.PP
See
B
Bruce Momjian 已提交
403
.IR pgintro (libpq)
404 405 406
for additional details.
.SH "RETURN VALUE"
.IR psql
407 408
returns 0 to the shell on successful completion of all queries,
1 for errors, 2 for abrupt disconnection from the backend.
409 410 411 412
.IR psql
will also return 1 if the connection to a database could not be made for
any reason.
.SH "SEE ALSO"
B
Bruce Momjian 已提交
413
libpq(3),
414 415 416
monitor(1)
postgres(1),
postmaster(1).
417