\g or terminate with semicolon to execute query
\q to quit
-testdb=>
+testdb=>
types, relations (tables, views, indexes, sequences, large
objects), rules, and triggers.) For example:
-=> \dd version
+=> \dd version
Object descriptions
Schema | Name | Object | Description
------------+---------+----------+---------------------------
space and followed by a newline. This can be useful to
intersperse information in the output of scripts. For example:
-=> \echo `date`
+=> \echo `date`
Tue Oct 26 21:40:57 CEST 1999
If the first argument is an unquoted -n the trailing
large object. Optionally, it associates the given
comment with the object. Example:
-foo=> \lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'
+foo=> \lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'
lo_import 152801
The response indicates that the large object received object ID
\set:
-testdb=> \set foo bar
+testdb=> \set foo bar
sets the variable foo to the value
bar. To retrieve the content of the variable, precede
the name with a colon and use it as the argument of any slash
command:
-testdb=> \echo :foo
+testdb=> \echo :foo
bar
this is again to prepend the variable name with a colon
(:).
-testdb=> \set foo 'my_table'
-testdb=> SELECT * FROM :foo;
+testdb=> \set foo 'my_table'
+testdb=> SELECT * FROM :foo;
would then query the table my_table. The value of
the variable is copied literally, so it can even contain unbalanced
copy the contents of a file into a table column. First load the file into a
variable and then proceed as above.
-testdb=> \set content '\'' `cat my_file.txt` '\''
-testdb=> INSERT INTO my_table VALUES (:content);
+testdb=> \set content '\'' `cat my_file.txt` '\''
+testdb=> INSERT INTO my_table VALUES (:content);
One possible problem with this approach is that my_file.txt
might contain single quotes. These need to be escaped so that
they don't cause a syntax error when the second line is processed. This
could be done with the program sed:
-testdb=> \set content '\'' `sed -e "s/'/\\\\\\'/g" < my_file.txt` '\''
+testdb=> \set content '\'' `sed -e "s/'/\\\\\\'/g" < my_file.txt` '\''
Observe the correct number of backslashes (6)! It works
this way: After
psql has parsed this
%]. Multiple pairs of these may occur within
the prompt. For example,
-testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%#%] '
+testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%#%] '
results in a boldfaced (1;) yellow-on-black
(33;40) prompt on VT100-compatible, color-capable
discouraged. If you get strange messages, keep this in mind.
For example
-testdb=> \foo
+testdb=> \foo
Field separator is "oo".
which is perhaps not what one would expect.
The first example shows how to spread a command over several lines of
input. Notice the changing prompt:
-testdb=> CREATE TABLE my_table (
+testdb=> CREATE TABLE my_table (
testdb(> first integer not null default 0,
testdb(> second text)
-testdb-> ;
+testdb-> ;
CREATE TABLE
Now look at the table definition again:
-testdb=> \d my_table
+testdb=> \d my_table
Table "my_table"
Attribute | Type | Modifier
-----------+---------+--------------------
Now we change the prompt to something more interesting:
-testdb=> \set PROMPT1 '%n@%m %~%R%# '
-peter@localhost testdb=>
+testdb=> \set PROMPT1 '%n@%m %~%R%# '
+peter@localhost testdb=>
Let's assume you have filled the table with data and want to take a
look at it:
-peter@localhost testdb=> SELECT * FROM my_table;
+peter@localhost testdb=> SELECT * FROM my_table;
first | second
-------+--------
1 | one
You can display tables in different ways by using the
\pset command:
-peter@localhost testdb=> \pset border 2
+peter@localhost testdb=> \pset border 2
Border style is 2.
-peter@localhost testdb=> SELECT * FROM my_table;
+peter@localhost testdb=> SELECT * FROM my_table;
+-------+--------+
| first | second |
+-------+--------+
+-------+--------+
(4 rows)
-peter@localhost testdb=> \pset border 0
+peter@localhost testdb=> \pset border 0
Border style is 0.
-peter@localhost testdb=> SELECT * FROM my_table;
+peter@localhost testdb=> SELECT * FROM my_table;
first second
----- ------
1 one
4 four
(4 rows)
-peter@localhost testdb=> \pset border 1
+peter@localhost testdb=> \pset border 1
Border style is 1.
-peter@localhost testdb=> \pset format unaligned
+peter@localhost testdb=> \pset format unaligned
Output format is unaligned.
-peter@localhost testdb=> \pset fieldsep ","
+peter@localhost testdb=> \pset fieldsep ","
Field separator is ",".
-peter@localhost testdb=> \pset tuples_only
+peter@localhost testdb=> \pset tuples_only
Showing only tuples.
-peter@localhost testdb=> SELECT second, first FROM my_table;
+peter@localhost testdb=> SELECT second, first FROM my_table;
one,1
two,2
three,3
Alternatively, use the short commands:
-peter@localhost testdb=> \a \t \x
+peter@localhost testdb=> \a \t \x
Output format is aligned.
Tuples only is off.
Expanded display is on.
-peter@localhost testdb=> SELECT * FROM my_table;
+peter@localhost testdb=> SELECT * FROM my_table;
-[ RECORD 1 ]-
first | 1
second | one