psql is an interactive terminal to the PostgreSQL database.
When you type
psql at the prompt you get a window, where you "talk" to the database. In addition to standard SQL queries (SELECT FROM ... , UPDATE ... , CREATE TABLE ..., etc, etc) you have many special commands you can use. They called meta-commands and they all begin with back-slash.
For example
\d - display all tables and views in my database
\d tab-name - describe table tab-name
\d+ tab-name - same as above, but more details.
To exit the psql session type \q
To avoid passing extra arguments when invoking psql you can set your envoronmental variables PGUSER, PGDATA and PGDATABASE.
Sometimes you don't need to stay in the psql session, you can call psql with one command, it'll tell you what you need and exit back to the command prompt.
For example, I want to see all databases on my PostgreSQL server:
-
C:\Temp> psql -l
-
Password: ******
-
List of databases
-
Name | Owner | Encoding
-
-----------+----------+----------
-
Michael | Michael | UTF8
-
postgres | postgres | UTF8
-
template0 | postgres | UTF8
-
template1 | postgres | UTF8
-
(4 rows)
-
-
C:\Temp>
-
In a similar fashion you can call psql with a query. It will execute it and spit back the result:
-
C:\Temp> psql -c "select count(1) from pg_ts_cfg"
-
Password: ******
-
count
-
-------
-
4
-
(1 row)
-
-
C:\Temp>
-
You'll find more interesting things in the psql
man page