Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Databases - Practical PostgreSQL
Previous Page Home Next Page

Slash Commands

Recall that within psql you have several special commands, called slash commands. These commands are psql -specific, and are not sent to the PostgreSQL backend. Explanations of the available psql slash commands follow.

Formatting commands

There are several slash commands available to format output. These include \pset, \a, \C, \f, \H, \t, \T, and \x. Except for \pset, each command controls a different formatting option. The \pset command, which is newer than the others, controls most of those same settings. The other commands exist for compatibility with older versions, and for convenience.

Most of these duplicate the effects of \pset. Each command is detailed within the description of that command and its options. For compatibility with older versions, and convenience, some of these formatting options may still have a slash command devoted entirely to them; these commands have been listed as well.

\pset parameter [ value ]

The general parameter setting command; this is the most important (and powerful) formatting command of the list. It encapsulates a variety of display options, and it could easily be the only formatting slash command you ever use. You may pass it various parameters to accomplish different formatting functions.

Within its syntax, parameter is one of the following valid parameters:

format

This parameter lets you set the output format to aligned , unaligned , html , or latex . Aligned is the default setting, for readability. Unaligned will set output to be printed all on one line, separated by the current character delimiter. The HTML and LaTeX modes output tables meant for inclusion in HTML and LaTeX documents, respectively.

border

Depending on the formatting mode, this option will make various changes to the borders used within displayed tables. For example, when outputting in HTML mode, this directly affects the border attribute of the <table> tag. This parameter takes a numeric value. Generally, the higher this number is, the larger (or more pronounced) the borders will be.

expanded

Setting this option will toggle between regular and extended format. If you have problems with data being displayed off the screen, or wrapping around in an illegible fashion, try using this option. It will tell psql to format all output into two columns, with the column name on the left, and data on the right.

null

This parameter allows you to set the string that is displayed to you when a null field is displayed. The string you wish to have displayed to represent a null should follow the word null. Ordinarily, that string is set to nothing. To set it back to nothing, you may set it with two apostrophes in a row (''). To set it to some other value, enclose that value in single-quotes. For example: \pset null ' ***null*** '.

fieldsep

This parameter accepts the delimiter to separate column values when working in the unaligned formatting mode. It is set to the pipe symbol (|) by default. You may want to use this to set the delimiter to a more commonly used delimiter, such as the tab (\t) character or comma (,). This has no effect outside of unaligned mode.

recordsep

This parameter specifies the record delimiter (to separate rows) when working in unaligned formatting mode. By default this is the newline character (\n).

tuples_only

This parameter lets you specify whether you want to see table data only (row results), or if you want to see additional characteristics about the table, such as headers and comments.

title

This parameter is used to attach a title to any subsequently printed titles. It will be displayed just above normal output. Use a pair of sequential apostrophes ('') to set to an empty string.

tableattr

This parameter is for use with the HTML format mode; use it to define any table attributes you wish to be included upon formatting table output within the <table> tag (e.g., width, cellpadding, cellspacing). If you wish to define more than a single attribute, be sure to enclose them within double-quotes in a single value .

pager

This parameter toggles off and on the use of a pager for outputting table data. You may set the PAGER environment variable in your shell before starting psql if you wish to use a paging program other than more (such as less ).

\a

The align command; this toggles psql between aligned and unaligned mode. This is equivalent to successive uses of \pset format aligned and \pset format unaligned.

\C

The query title command; this allows you to set a title that will be displayed at the top of any displayed result set, and is equivalent to \pset title.

\f

The field delimiter command; this sets the field delimiter when using the unaligned formatting mode, and is equivalent to \pset fieldsep.

\H

The HTML output command; this toggles between HTML output formatting and the default aligned formatting, and is equivalent to successive uses of \pset format HTML and \pset format aligned.

\t

The table information command; this toggles the display of optional table information, and is equivalent to \pset tuples_only.

\T

The table attribute command; this defines extra table attributes you wish to be inserted into the table tags of table data displaying while in HTML formatting mode. It is equivalent to \pset tableattr.

\x

The toggle expanded command; this toggles expanded row formatting and off. It is equivalent to \pset expanded.

Information display commands

The psql client has many commands to help you with gathering information about the database and various objects within it. Most of these commands are prefixed with \d, as this is an easy mnemonic device for display . Knowing how to use these commands can increase your productivity (or at least your awareness!) within the database.

\d [ relation_name ]

The general display command; it is used to view various pieces of information about a specified relation. The relation you specify may be an index, sequence, table, or view. When issued, the command will display all of the relation's columns, types, and special attributes or defaults. When executed without a specified relation, it displays each of the relations available within the currently connected database.

\da [ aggregate_name ]

The aggregate display command; with it, you may retrieve the list of the connected database's aggregate functions, and their accepted data types. If you specify a parameter following the slash command, it will display only the list of aggregate functions whose names begin with the aggregate_name pattern in a case-insensitive comparison.

\dd [ name ]

The general database object display command; it is used to display the descriptions of any specified database object. The object you specify may be any defined aggregate, function, operator, relation, rule, or trigger. If you do not specify an object name , or a partial name, all objects in the database will be displayed.

\df [ function_name ]

The function display command; it is used to display information about a function defined within the database, including its arguments, and return types. You can either specify a function to display, or specify none, and list information about all functions. Like \da and \dd, a full or partial function_name may be supplied for a case-insensitive comparison against all functions from the beginning of each function name.

\d[istvS] [ name ]

A scoped version of the general display command; you may specify any of the options within the brackets:

i

Displays indices.

s

Displays sequences.

t

Displays tables.

v

Displays views.

S

Displays system tables.

\dl

The large object display command; this command is equivalent to the \lo_list command, which displays the list of large objects within the current database.

\do [ operator_name ]

The operator display command; this displays the list of defined operators within the current database, along with their operands (arguments), and return types. You may specify a complete or partial operator_name to examine, or retrieve information about all available operators.

\dp [ object_name ]

The permissions display command; this retrieves the list of all database objects (or objects at least partially matching an object_name , if provided) currently defined within the database, along with all their associated access permissions (public, user, and group).

\dT [ type_name ]

The data type display command; this displays the list of all available data types. You may again specify a type_name , or partial data type name, or view all available data types in the current database.

\l

The database display command; this lists all defined databases on the server, and their ownership information, and multibyte encoding type. Entering \l+ will display any comments the databases may have (see the Section called Documenting a Database in Chapter 9" in Chapter 9 for how to comment on a database).

\lo_list

The large object display command; this displays the list of all existing large objects within the current database, along with any comments that have been attached to them.

\z [ object_name ]

The permissions display command, equivalent to \dp.

PostgreSQL and psql informative commands

Within psql there is a small set of informative commands that display information about PostgreSQL and psql itself. These are useful primarily for obtaining help with command-related questions you may have.

\?

The help command; this prints out the list of slash commands documented in this chapter.

\copyright

The copyright command; this displays copyright information about PostgreSQL.

\encoding

The encoding command; if multibyte encoding is enabled, this can set the client encoding. If you do not supply an argument, the current encoding will be displayed on the screen.

\help

The general help command; used without an argument, it will print a list of all commands for which greater help is available. Used with an argument, it will print more information (if there is data available) for the subject. Used with an asterisk (*) as the argument, it will retrieve syntax information for all documented SQL commands.

Input and output commands

The psql client's various input and output slash commands allow you to transfer data to and from the database in different ways. You may also specify exactly how psql transfers data. The commands include:

\copy table { FROM | TO } file | stdin | stdout

The copy command; this can be used to copy from the client application (and thus, use the permissions of the user who started the client) instead of using the SQL COPY command to copy from the server. This slash command can also accept any of the standard COPY clauses. For more information on the syntax of this command, refer to the COPY entry in the command reference section at the back of this book.

The difference between using \copy over COPY are important to understand and include:

  • Data you \copy transfers first through the client (via your connection), which may be quite a bit slower than if it were done directly through the server (i.e., the backend) process.

  • You have access to files on the local filesystem under whatever permissions the user account you are using has, which means you may have more (or less) accessibility to needed files than the backend process.

  • The terms stdin and stdout (standard input and output) have a different meaning; they refer to psql  's input and output stream. On the backend process they are used differently: stdin represents where the COPY was issued from, and stdout represents the query output stream.

\echo string

The echo command; this sends a string to the standard output. This can be useful for scripting, because you can add non-database–supplied information into script output (such as comments).

\g [ file ]

The buffer execution command; this is essentially the same as using the semicolon (;) in that it sends the current query buffer to the backend to be processed. Optionally, you can save the result set to a file of your choice, or have psql pipe it to a separate shell command by following the \g with either a filename or piped command name.

\i file

The file input command; this reads input from a file (the name of which you supply as an argument after the \i) and causes psql to parse its content as if it were typed directly into the program's prompt.

\lo_export lo_oid filename

The large object export command; this lets you export the large object with OID lo_oid to filename on your local filesystem. This is different from the lo_export() server function in the same way the \copy and the SQL COPY commands are different.

\lo_import filename [ comment ]

The large object import command; this imports large objects into the database from files on your local filesystem. Optionally, you can attach a comment to the object; this is recommended, as otherwise it will be identifiable only by an OID, which you will need to remember if you wish to access it again. If you attach a comment to the object, issuing the \lo_list command displays your comment with the OID of the object, thus making it easier to find once imported.

\o [ file | |command ]

The output command; this redirects future output (i.e., data retrieved after this command is issued) to either a file of your choice or a pipe to a system command . If not given any arguments, the output channel will reset to standard output; use no arguments when you wish to stop sending output elsewhere. One of the most useful features of this command is the ability to pipe output to commands such as grep , which can then search for a pattern of your choosing, allowing you to search against database and slash command output (which will, of course, only work if grep is installed on your system).

\p

The buffer display command; this prints the psql input currently buffered. If no SQL input has been entered since the last executed statement, the last executed statement is displayed.

\qecho string

The query-output echo command; this sends a string to your chosen query output channel (which is set with the \o command), instead of stdout. This command can be useful when you need to send non-database–related information into the psql output.

\w file | |command

The buffer output command; this outputs the current query buffer to a specified file , or piped system command .

System commands

The following commands pertain to the general, systematic functions of psql . These include database re-connection, external editor invocation, setting and unsetting psql variables, and quitting psql .

\connect [ database [ username ] ]

The database re-connection command; this connects you to another database from within psql . You may specify the database to connect to and the username to use (if it is not the same as the current username) and omitting this parameter will cause the current username to be used.

\edit [ file ]

The external editor command; with this, you can either edit a file of your choice or (if no file is specified) the current query buffer. After you are done editing, the new buffer is input to the query buffer, and executed if terminated with a semi-colon.

When opening a file for editing with this command, psql searches your environment variables for the following fields (in this order) to find out what editor to use: PSQL_EDITOR, EDITOR, and VISUAL. If none of these are present, it will attempt to launch /bin/vi .

\q

The quit command; this exits the program. You may also use CTRL-D in most terminal applications to quit.

\set [ name [ value ] ]

The variable setting command; used without arguments, this displays all set variables within psql . Otherwise, it sets the variable name to value . If no value is passed, name is set with an empty value. If multiple values are passed, name is assigned the concatenation of each value .

\unset variable

The variable unsetting command; this unsets a specified variable from memory. This is different from assigning a variable with an empty value, which is still technically set.

\! [ command ]

The shell execution command; without arguments, this opens a shell which overrides the psql prompt until it is exited. Otherwise, it executes a specified shell command from within psql , and displays its results to stdout.

Databases - Practical PostgreSQL
Previous Page Home Next Page

 
 
  Published under the terms of the Open Publication License Design by Interspire