postgresql_16:7.2.18.9 Examples
PostgreSQL 16 > Reference > PostgreSQL Client Applications > psql > Examples
The first example shows how to spread a command over several lines of input. Notice the changing prompt: testdb=> CREATE TABLE my_table ( testdb(> first integer not null default 0, testdb(> second text) testdb-> ; CREATE TABLE Now look at the table definition again: testdb=> \d my_table Table "public.my_table" Column | Type | Collation | Nullable | Default --------+---------+-----------+----------+--------- first | integer | | not null | 0 second | text | | | Now we change the prompt to something more interesting: 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; first | second -------+-------- 1 | one 2 | two 3 | three 4 | four (4 rows) You can display tables in different ways by using the \pset command: peter@localhost testdb=> \pset border 2 Border style is 2. peter@localhost testdb=> SELECT * FROM my_table; +-------+--------+ | first | second | +-------+--------+ | 1 | one | | 2 | two | | 3 | three | | 4 | four | +-------+--------+ (4 rows) peter@localhost testdb=> \pset border 0 Border style is 0. peter@localhost testdb=> SELECT * FROM my_table; first second ----- ------ 1 one 2 two 3 three 4 four (4 rows) peter@localhost testdb=> \pset border 1 Border style is 1. peter@localhost testdb=> \pset format csv Output format is csv. peter@localhost testdb=> \pset tuples_only Tuples only is on. peter@localhost testdb=> SELECT second, first FROM my_table; one,1 two,2 three,3 four,4 peter@localhost testdb=> \pset format unaligned Output format is unaligned. peter@localhost testdb=> \pset fieldsep '\t' Field separator is " ". peter@localhost testdb=> SELECT second, first FROM my_table; one 1 two 2 three 3 four 4 Alternatively, use the short commands: 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; -[ RECORD 1 ]- first | 1 second | one -[ RECORD 2 ]- first | 2 second | two -[ RECORD 3 ]- first | 3 second | three -[ RECORD 4 ]- first | 4 second | four
Also, these output format options can be set for just one query by using \g: peter@localhost testdb=> SELECT * FROM my_table peter@localhost testdb-> \g (format=aligned tuples_only=off expanded=on) -[ RECORD 1 ]- first | 1 second | one -[ RECORD 2 ]- first | 2 second | two -[ RECORD 3 ]- first | 3 second | three -[ RECORD 4 ]- first | 4 second | four
Here is an example of using the \df command to find only functions with names matching int*pl and whose second argument is of type bigint: testdb=> \df int*pl * bigint List of functions Schema | Name | Result data type | Argument data types | Type ------------+---------+------------------+---------------------+------ pg_catalog | int28pl | bigint | smallint, bigint | func pg_catalog | int48pl | bigint | integer, bigint | func pg_catalog | int8pl | bigint | bigint, bigint | func (3 rows)
When suitable, query results can be shown in a crosstab representation with the \crosstabview command: testdb=> SELECT first, second, first > 2 AS gt2 FROM my_table; first | second | gt2 -------+--------+----- 1 | one | f 2 | two | f 3 | three | t 4 | four | t (4 rows) testdb=> \crosstabview first second first | one | two | three | four -------+-----+-----+-------+------ 1 | f | | | 2 | | f | | 3 | | | t | 4 | | | | t (4 rows) This second example shows a multiplication table with rows sorted in reverse numerical order and columns with an independent, ascending numerical order. testdb=> SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB", testdb(> row_number() over(order by t2.first) AS ord testdb(> FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC testdb(> \crosstabview "A" "B" "AxB" ord A | 101 | 102 | 103 | 104 ---+-----+-----+-----+----- 4 | 404 | 408 | 412 | 416 3 | 303 | 306 | 309 | 312 2 | 202 | 204 | 206 | 208 1 | 101 | 102 | 103 | 104 (4 rows)