02 Lesson 02: Basic Tools / Using psql
This page is extracted from the official PDF into native Markdown. Slide text, code and speaker notes are kept in reading order; the original PDF and HTML remain the layout references. HTML source: dba1_02_tools_psql.html Archive: DBA1-handouts-16-en.zip PDF pages: 17
Page 1: Basic Tools / Using psql
Copyright
© Postgres Professional, 2017–2025 Authors: Egor Rogov, Pavel Luzanov, Ilya Bashtanov, Aleksei Beresnev Translated by: Liudmila Mantrova, Alexander Meleshko, Elena Sharafutdinova Photo: Oleg Bartunov (Phu Monastery and Bhrikuti Peak, Nepal)
Use of Course Materials
Non-commercial use of course materials (presentations, demonstrations) is allowed without restrictions. Commercial use is possible only with the written permission of Postgres Professional. It is prohibited to make changes to the course materials.
Feedback
Please send your feedback, comments and suggestions to: edu@postgrespro.ru
Disclaimer
Postgres Professional assumes no responsibility for any damages and losses, including loss ofincome, caused by direct or indirect, intentional or accidental use of course materials. Postgres Professional company specifically disclaims any warranties on course materials. Course materials are provided “as is,” and Postgres Professional company has no obligations to provide maintenance, support, updates, enhancements, or modifications.
Page 2: Topics
- Launching psql and Connecting to the Database
- Getting Help
- Working with psql
- Configuring psql
Page 3: Purpose of psql
Terminal client for working with PostgreSQL
- Comes with the DBMS
- Used by administrators and developers for interactive work and script execution
Speaker notes
There are other third-party tools available, but they are not considered in the scope of the course.
The psql terminal client will be used throughout the course. Those who are used to working with GUI tools may find it uncomfortable at first. Nevertheless, it is very powerful if you get used to it.
This is the only client supplied with the DBMS. The knowledge of psql will be useful to both developers and DB administrators, regardless of which tool they choose to work with at the end of the day.
https://postgrespro.com/docs/postgresql/16/app-psql
Page 4: Connection
Launch
- New connection in psql
- Information about the current connection
Speaker notes
The required connection parameters include: database name, user name, server name, port number. If these parameters are not specified, psql will try to connect using the default values:
● database — matches the user name;
● user — matches the OS user name;
● node — connection via Unix socket;
● port — usually 5432.
To make a new connection without leaving psql, run the \connect command. It uses the current connection’s parameters as defaults.
The \conninfo command provides information about the current connection.
Additional information about connection configuration options:
https://postgrespro.com/docs/postgresql/16/libpq-envars
https://postgrespro.com/docs/postgresql/16/libpq-pgservice
https://postgrespro.com/docs/postgresql/16/libpq-pgpass
Page 5: Getting Help
In the OS command line
- In psql
- => \?
- list of psql commands
- => \? variables
- psql variables
- => \h[elp]
- list of SQL commands
- => \h command
- syntax of the SQL command
- => \q
- quit
Speaker notes
Reference information on psql can be obtained not only from the documentation, but also from within the system directly.
psql with the –help option displays a startup help message. If the documentation package is installed with the system, you can view the manual for psql using the man psql command.
psql can execute SQL commands as well as its own commands. All psql commands start with a backslash and, as a rule, can be abbreviated to their first letter.
Inside psql, you can get a list and a brief description of all psql commands: \?.
The \help command provides a list of SQL commands that the server supports, as well as the syntax of an SQL command (if specified).
Another command that is useful to know, although it has nothing to do with the help, is \q — exit psql. Alternatively, you can use the exit and quit commands to quit.
Page 6: Executing SQL Commands and Formatting the Output
Run psql:
Check the connection:
Using the default parameters, we have connected to the student database as the student user. You will learn more about databases and users in later modules. The \c[onnect] command creates a new connection without leaving psql. SQL commands, unlike psql ones, may span multiple rows. To send an SQL command for execution, end it with a semicolon:
psql can give output in different formats. Here are some of them: Aligned format Unaligned format Extended format The aligned format is the default. It sets each column’s width based on its contents. There is also the header and the total row. psql commands to switch display modes: \a — switches aligned format and unaligned format mode \t — switches the header and footer display Let’s switch to non-aligned, turn the header and the total row off, and use a whitespace as the separator:
The extended format is convenient for displaying multiple columns for one or several records:
The extended mode can be set for a single query. To do that, add \gx at the end instead of a semicolon:
Page 7: Page 7
You can see all the formatting options by using the \pset command. If used with no parameters, it will display current settings:
Interacting with the OS
psql can run shell commands:
You can set OS environment variables:
It can write output into a file with the \o[ut] command:
There is nothing on the screen! Let’s check the file:
Let’s get the output back to the screen:
Executing Scripts
Another way to run a query is with the \g command. You can specify parameters for this particular query in the brackets. Query output can be redirected to OS command by appending the command after a vertical bar. For example, display query output with line numbers:
The \g command can specify a filename to redirect output:
Page 8: Page 8
This is what we get:
We can execute this file as a script using \i[nclude]:
Other ways to run commands from a file: psql < filename psql -f filename You can skip creating a file in the last example if you end the query with \gexec:
gexec considers the contents of each column of each row an SQL-operator, and tries to execute them one by one.
psql Variables and Control Structures
Not unlike shell, psql has its own variables, including some pre-defined ones (with special meaning for psql). Store the value of the OS USER environment variable in a psql variable:
Set a variable Test:
To substitute a variable’s value, use a colon prefix before the variable name:
To reset a variable, use:
Query results can be saved to variables. For this purpose use the \gset command termination:
The query must return only one record. If used with no parameters, \set displays all currently set variables and their values:
Page 9: Page 9
In scripts, you can use conditional operators. For example, let’s check if working_dir has a value, and if it does not, set it as the current directory. The following command checks if the value is set and returns a Boolean value:
This conditional psql operator checks if the variable exists and sets the default value if needed:
Now we can be sure that the working_dir variable is defined:
System Catalog Commands
There is a set of commands (mostly starting with \d) used to quickly and conveniently get information about database objects. Example:
We will see more of these commands later.
Configuring psql
On startup, psql runs two scripts (if they exist): First a common system psqlrc script Then a custom .psqlrc file
Page 10: Page 10
The user configuration file must be in the home directory. The system script’s location can be discovered with the following command:
Neither file exists by default. You can use the files to configure your session parameters, for example: psql prompt Program for page-by-page viewing of query results Variables for storing the text of frequently used commands For example, let’s store a query that returns 5 largest tables in a variable top5:
Now we can execute the query by just typing:
If you write the \set command into the ~/.psqlrc file, the top5 variable will be available immediately after psql startup. Thanks to readline support, psql can autocomplete keywords and object names, and it also stores the command history. The name and the size of the history file are set by HISTFILE and HISTSIZE variables.
Page 11: Takeaways
psql is a terminal client for working with PostgreSQL
- Connection parameters are required at startup
- Executes SQL and psql commands
- Includes tools for interactive work, as well as for preparing and executing scripts
Page 12: Practice
- Run psql and check the current connection information. 2. Display a detailed list of databases. 3. By default, psql uses less command for page-by-page output. Replace it with less -XS and display the detailed database list again. 4. The default prompt shows only the name of the database. Configure the prompt to display in the following format: user@database=#. 5. Configure psql to display the execution time for all commands. Make sure that this setting is saved when you restart.
Speaker notes
- When starting psql, if you omit the connection parameters, the default values will apply.
The pager program is configured using the PSQL_PAGER environment variable. The setting can be configured in the .psqlrc file using the \setenv command. This will set the value to ’less -XS’ specifically for psql sessions, while maintaining the OS default settings in all other cases. By default, less wraps long lines during viewing and clears its output upon exit. The -XS parameter disables this default behavior.
Prompt customization is described in the documentation:
https://postgrespro.com/docs/postgresql/16/app-psql#APP-PSQL-PROMPTI NG
5.The psql command to output the duration of a query execution can be found in the PostgreSQL documentation or within psql itself with the \? command.
Page 13: 1. Running psql and Displaying Connection Information
2. Paging Results Using less
When query output exceeds terminal dimensions, psql sends it to the less pager. You can navigate through query results using standard navigation keys. The h command displays the help file. The q command exits the display mode.
Note Note that by default less will wrap long lines, which can make the results difficult to read. Besides, the output will be cleared after quitting less. For example, \l+ output becomes unreadable due to wrapping.
3. Configuring Page View in .psqlrc
When using the less pager with -XS flags, long lines will not wrap and output remains visible after exiting the less pager. For such configuration, simply set the PSQL_PAGER environment variable using the \setenv command. We will save this setting in the ~/.psqlrc script:
4. Customizing Prompts
To include role information in your prompts, prepend %n@ to both PROMPT1 and PROMPT2 variables.
The PROMPT1 variable controls the primary prompt displayed for the first line of a user’s query input. For multi-line queries, PROMPT2 controls the prompt display from the second line onward. While both variables share identical default values, you can configure distinct prompts for initial and following lines. Note that PROMPT3 serves only for COPY command operations.
5. Output of SQL Execution Timing
The complete contents of your .psqlrc file will be like this:
Page 14: Page 14
For the changes to take effect, restart your psql session.
After restarting, verify the new configuration: Prompt (must include role name) Displaying detailed information about databases Output of command execution time
Page 15: Practice+
- Open a transaction and execute a command that ends with any error. Make sure that no other commands can be executed inside this transaction.
- Set the ON_ERROR_ROLLBACK parameter to on and make sure that after the error, you can continue executing commands inside the transaction.
Speaker notes
- To open a transaction, run the command
- Setting the ON_ERROR_ROLLBACK parameter to ON causes psql to create a SAVEPOINT before each SQL command inside an open transaction and, in case of an error, roll back to this savepoint.
https://postgrespro.com/docs/postgresql/16/sql-savepoint
Page 16: 1. psql and In-Transaction Errors
The psql tool autocommits transactions by default, so each SQL command is executed within a separate transaction. So each SQL command is executed within a separate transaction. To start a transaction explicitly, the BEGIN command is used:
Note Note that the psql prompt has changed. The asterisk character shows that the transaction is currently open.
Consider that we have made a typo in the following command:
The asterisk will change to an exclamation mark, indicating an error. Now, rewrite the command:
But PostgreSQL cannot roll back just a single command, so it terminates and rolls back the whole transaction. To continue, we must send a command that says that the transaction is complete. It can be either COMMIT or ROLLBACK, since the transaction is already cancelled.
Creating the table was cancelled, so there is no such table in the database:
The ON\_ERROR\_ROLLBACK Variable
We can change how psql behaves here.
Now, before every transaction command, there will be a savepoint created. In case of an error, it will roll back to the last savepoint. This way, transaction commands can continue executing.
Page 17: Page 17
The ON_ERROR_ROLLBACK variable can be set to interactive. This will make such behavior work only in the interactive mode, but not when executing scripts.