Sql

How do you use script variables in psql

27 September 2026 · 7 min read

How do you use script variables in psql

Navigating the powerful command-line interface of PostgreSQL, known as psql, offers database administrators and developers an indispensable tool for managing and interacting with their databases. While its raw power is evident, true efficiency often lies in its scripting capabilities. A core component of this efficiency is understanding how do you use script variables in psql? These variables allow you to store values, parameterize queries, and build dynamic, reusable scripts, significantly enhancing your workflow. Mastering them means moving beyond static SQL commands to a more adaptable and automated approach, reducing repetitive tasks and minimizing the potential for human error. This guide will delve into the various ways to define, manipulate, and leverage script variables within your psql sessions and scripts, transforming how you interact with your PostgreSQL databases.

Understanding psql Script Variables: The Basics

Psql script variables, often referred to as client-side variables or session variables, are distinct from server-side SQL variables (like those used in PL/pgSQL functions). They exist purely within the psql client’s environment, allowing for powerful scripting and dynamic query construction without affecting the database server’s state. These variables are incredibly useful for tasks such as storing file paths, connection strings, numerical thresholds, or even parts of SQL queries that change frequently.

The primary purpose of psql variables is to enable the creation of flexible and reusable scripts. Instead of hardcoding values directly into your SQL commands or meta-commands, you can define variables and reference them, making your scripts adaptable to different scenarios or environments. For instance, you could define a variable for a table name, then use that variable across multiple queries within a single script. This approach not only streamlines development but also makes scripts easier to maintain and debug.

To use script variables in psql, you define them using the \set meta-command and reference them with a colon prefix (e.g., :my_variable). This allows you to store values like strings, numbers, or even the results of SQL queries, making your psql scripts dynamic and reusable across various operations and environments. Understanding these fundamental operations is key to unlocking advanced scripting techniques in psql.

Defining and Manipulating Variables with \set and \unset

The cornerstone of managing psql script variables is the \set meta-command. This command assigns a value to a variable within your psql session. The syntax is straightforward: \set variable_name value. The value can be a string, a number, or even the output of another command or a SQL query. For example, you might set a variable to hold the current date, a database name, or a user ID for a series of operations.

When the value includes spaces or special characters, it should be enclosed in single quotes. If you need to store the result of a SQL query into a variable, you can use backticks or parentheses with the \set command, like \set my_count SELECT COUNT() FROM users; or \set my_db (SELECT current_database());. This capability is particularly powerful, allowing you to dynamically adapt your script’s behavior based on real-time database conditions.

To remove a variable from your psql session, you use the \unset meta-command. The syntax is simply \unset variable_name. This is useful for cleaning up your environment or for ensuring that a variable isn’t accidentally reused with an outdated value. It’s good practice to unset variables when they are no longer needed, especially in long or complex scripts, to maintain clarity and prevent unintended side effects.

Here are some common ways to use \set:

  • Setting a simple string: \set my_table 'employees'
  • Setting a number: \set limit_rows 100
  • Setting from a query result: \set user_id SELECT id FROM users WHERE username = 'admin';
  • Setting from an environment variable: \set backup_dir :ENV_PG_BACKUP_DIR

Once a variable is set, you reference it in your SQL queries or other psql meta-commands by prefixing its name with a colon, for example, SELECT FROM :my_table LIMIT :limit_rows;. Psql will substitute the variable’s value before sending the query to the server, making your SQL dynamic and highly adaptable.

Advanced Techniques: Dynamic Queries and Conditional Logic

Beyond simple value substitution, psql script variables unlock advanced scripting techniques, primarily dynamic query construction and conditional execution. Dynamic queries are essential when parts of your SQL statements need to change based on runtime conditions, user input, or data retrieved from the database itself. For instance, you might want to construct a WHERE clause dynamically based on user-defined filters.

To build dynamic SQL with variables, you concatenate strings and variables. While psql doesn’t have direct string concatenation operators like some programming languages, you can achieve this by setting variables with parts of your query and then combining them. For more complex dynamic SQL, especially when dealing with identifiers or values that might contain special characters, the \gexec meta-command can be invaluable. It takes the result of a query (which should be a valid SQL statement) and executes it, providing extreme flexibility.

Conditional logic is another powerful feature. Psql offers meta-commands like \if, \elif, \else, and \endif that allow you to execute blocks of commands based on certain conditions. These conditions can often involve checking the values of variables. For example, you could check if a specific variable is set, or if its value matches a certain string, to decide which set of SQL commands to run. This is extremely useful for creating robust scripts that can adapt to different database states or user preferences.

Consider a scenario where you’re building a script to manage database backups. You might use a variable to store the target directory, and then use conditional logic to ensure the directory exists before attempting the backup. Or perhaps you need to run different sets of migrations based on the current database version. This level of control makes your scripts significantly more intelligent and resilient. For more insights on optimizing database operations, consider exploring advanced PostgreSQL performance tuning.

  • Dynamic Table Selection: \set table_name 'audit_log' then SELECT FROM :table_name WHERE event_date > current_date - interval '7 days';
  • Conditional Execution: \if :environment = 'production' \echo 'Running in production!' \else \echo 'Running in development.' \endif
  • Executing Dynamic SQL: \set sql_query 'SELECT version();' then :sql_query\gexec<b>Question & Answer : </b><br></br><p>In MS SQL Server, I create my scripts to use customizable variables:</p> <pre>DECLARE @somevariable int SELECT @somevariable = -1 INSERT INTO foo VALUES ( @somevariable ) </pre> <p>I'll then change the value of @somevariable at runtime, depending on the value that I want in the particular situation. Since it's at the top of the script it's easy to see and remember.</p> <p>How do I do the same with the PostgreSQL client psql?</p><br></br><p>Postgres variables are created through the \set command, for example ...</p> <pre>\set myvariable value </pre> <p>... and can then be substituted, for example, as ...</p> <pre>SELECT * FROM :myvariable.table1; </pre> <p>... or ...</p> <pre>SELECT * FROM table1 WHERE :myvariable IS NULL; </pre> <p><em>edit: As of psql 9.1, variables can be expanded in quotes as in:</em> </p> <pre>\set myvariable value SELECT * FROM table1 WHERE column1 = :'myvariable'; </pre> <p><em>In older versions of the psql client:</em> </p> <p>... If you want to use the variable as the value in a conditional string query, such as ...</p> <pre>SELECT * FROM table1 WHERE column1 = ':myvariable'; </pre> <p>... then you need to include the quotes in the variable itself as the above will not work. Instead define your variable as such ...</p> <pre>\set myvariable 'value' </pre> <p>However, if, like me, you ran into a situation in which you wanted to make a string from an existing variable, I found the trick to be this ...</p> <pre>\set quoted_myvariable '\'' :myvariable '\'' </pre> <p>Now you have both a quoted and unquoted variable of the same string! And you can do something like this ....</p> <pre>INSERT INTO :myvariable.table1 SELECT * FROM table2 WHERE column1 = :quoted_myvariable; </pre>