PostgreSQL - isel-leic-ls/2122-2-common GitHub Wiki
Some notes on using PostgreSQL and the psql command line interface.
-
psqlis a command line tool that can be used to:- Connect to a PostgreSQL server.
- Execute SQL commands on that server.
- Execute scripts from the command line.
-
Use
psql <database> -h <host> -U <username>to create an interactive shell.- The password can be defined on the
PGPASSWORDenvironment variable. - The user name can be defined on the
PGUSERenvironment variable.
- The password can be defined on the
-
Use `psql -f <script_file> to execute a script.
-
The following commands are available inside the
psqlinteractive shell.-
\llists all databases. -
\cconnects to a database. -
\dlists all relations (e.g. tables). -
\iexecutes a script. -
\qexits the interactive shell.
-
-
SQL commands can be executed directly in the interactive shell, ended with ';'.