Contents /
Previous /
Next
The PostgreSQL Client: psql
The PostgreSQL interactive terminal program, called "psql"
allows you to interactively enter, edit, and execute SQL commands.
You have to specify the database you want to connect to
when you start psql.
It is not possible to access more than one database per connection.
Try:
shell> psql testbase
or
shell> psql template1
In psql, you will be greeted with the following message:
Welcome to psql, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
template1=#
The last line printed out by psql is the SQL prompt, try:
template1=# SELECT version();
version
----------------------------------------------------------------------
PostgreSQL 7.2.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.2
(1 row)
template1=# SELECT current_date;
date
------------
2003-03-11
(1 row)
template1=# SELECT CURRENT_USER;
current_user
--------------
felix
(1 row)
template1=# SELECT 2 + 2;
?column?
----------
4
(1 row)
Inside psql [TAB] can be used for autocompletion of SQL commands.
The psql program has a number of internal commands (not SQL).
They begin with the backslash character, "\". Some of these
commands were listed in the welcome message. For example:
\h -- help on the syntax of various PostgreSQL SQL
\? -- show internal psql commands like
\r -- reset (clear) the query buffer
\dT -- show all data types
\l -- list all databases
\dt -- list tables
\dS -- list system tables
\z -- check user rights for all tables in DB
\connect databasename user -- connect to other DB
\q -- quit psql
For more internal commands check also "man psql".