↑↓ select ↵ open ⌫ change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

Wiki / psql Commands / Scripting and assistance

\else

This group of commands implements nestable conditional blocks. A conditional block must begin with an \if and end with an \endif . In between there may be any number of \elif clauses, which may optionally be followed by a single \else clause. Ordina…

Reading PostgreSQL 18.6.

Description

This group of commands implements nestable conditional blocks. A conditional block must begin with an \if and end with an \endif . In between there may be any number of \elif clauses, which may optionally be followed by a single \else clause. Ordinary queries and other types of backslash commands may (and usually do) appear between the commands forming a conditional block.

Client
psql 18.6
Case-sensitive spelling
\else

Usage

\if expression \elif expression \else \endif

Spellings in this manual definition

CommandDocumented suffix modifiers
\ifNone listed in this signature
\elifNone listed in this signature
\elseNone listed in this signature
\endifNone listed in this signature

Manual definition

\if expression
\elif expression
\else
\endif

This group of commands implements nestable conditional blocks. A conditional block must begin with an \if and end with an \endif. In between there may be any number of \elif clauses, which may optionally be followed by a single \else clause. Ordinary queries and other types of backslash commands may (and usually do) appear between the commands forming a conditional block.

The \if and \elif commands read their argument(s) and evaluate them as a Boolean expression. If the expression yields true then processing continues normally; otherwise, lines are skipped until a matching \elif, \else, or \endif is reached. Once an \if or \elif test has succeeded, the arguments of later \elif commands in the same block are not evaluated but are treated as false. Lines following an \else are processed only if no earlier matching \if or \elif succeeded.

The expression argument of an \if or \elif command is subject to variable interpolation and backquote expansion, just like any other backslash command argument. After that it is evaluated like the value of an on/off option variable. So a valid value is any unambiguous case-insensitive match for one of: true, false, 1, 0, on, off, yes, no. For example, t, T, and tR will all be considered to be true.

Expressions that do not properly evaluate to true or false will generate a warning and be treated as false.

Lines being skipped are parsed normally to identify queries and backslash commands, but queries are not sent to the server, and backslash commands other than conditionals (\if, \elif, \else, \endif) are ignored. Conditional commands are checked only for valid nesting. Variable references in skipped lines are not expanded, and backquote expansion is not performed either.

All the backslash commands of a given conditional block must appear in the same source file. If EOF is reached on the main input file or an \include-ed file before all local \if-blocks have been closed, then psql will raise an error.

Here is an example:

-- check for the existence of two separate records in the database and store
-- the results in separate psql variables
SELECT
    EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer,
    EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee
\gset
\if :is_customer
    SELECT * FROM customer WHERE customer_id = 123;
\elif :is_employee
    \echo 'is not a customer but is an employee'
    SELECT * FROM employee WHERE employee_id = 456;
\else
    \if yes
        \echo 'not a customer or employee'
    \else
        \echo 'this will never print'
    \endif
\endif

Related entries

Documentation and source

Source build
Version
18.6
Build
https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
Source fingerprint
ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8

Compare versions

PostgreSQL 18 → 19: changed.

--- PostgreSQL 18
+++ PostgreSQL 19
@@ -1,5 +1,5 @@
 {
-  "definition": "This group of commands implements nestable conditional blocks. A conditional block must begin with an \\if and end with an \\endif . In between there may be any number of \\elif clauses, which may optionally be followed by a single \\else clause. Ordinary queries and other types of backslash commands may (and usually do) appear between the commands forming a conditional block. The \\if and \\elif commands read their argument(s) and evaluate them as a Boolean expression. If the expression yields true then processing continues normally; otherwise, lines are skipped until a matching \\elif , \\else , or \\endif is reached. Once an \\if or \\elif test has succeeded, the arguments of later \\elif commands in the same block are not evaluated but are treated as false. Lines following an \\else are processed only if no earlier matching \\if or \\elif succeeded. The expression argument of an \\if or \\elif command is subject to variable interpolation and backquote expansion, just like any other backslash command argument. After that it is evaluated like the value of an on/off option variable. So a valid value is any unambiguous case-insensitive match for one of: true , false , 1 , 0 , on , off , yes , no . For example, t , T , and tR will all be considered to be true . Expressions that do not properly evaluate to true or false will generate a warning and be treated as false. Lines being skipped are parsed normally to identify queries and backslash commands, but queries are not sent to the server, and backslash commands other than conditionals ( \\if , \\elif , \\else , \\endif ) are ignored. Conditional commands are checked only for valid nesting. Variable references in skipped lines are not expanded, and backquote expansion is not performed either. All the backslash commands of a given conditional block must appear in the same source file. If EOF is reached on the main input file or an \\include -ed file before all local \\if -blocks have been closed, then psql will raise an error. Here is an example: -- check for the existence of two separate records in the database and store -- the results in separate psql variables SELECT EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer, EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee \\gset \\if :is_customer SELECT * FROM customer WHERE customer_id = 123; \\elif :is_employee \\echo 'is not a customer but is an employee' SELECT * FROM employee WHERE employee_id = 456; \\else \\if yes \\echo 'not a customer or employee' \\else \\echo 'this will never print' \\endif \\endif",
+  "definition": "This group of commands implements nestable conditional blocks. A conditional block must begin with an \\if and end with an \\endif . In between there may be any number of \\elif clauses, which may optionally be followed by a single \\else clause. Ordinary queries and other types of backslash commands may (and usually do) appear between the commands forming a conditional block. The \\if and \\elif commands read their argument(s) and evaluate them as a Boolean expression. If the expression yields true then processing continues normally; otherwise, lines are skipped until a matching \\elif , \\else , or \\endif is reached. Once an \\if or \\elif test has succeeded, the arguments of later \\elif commands in the same block are not evaluated but are treated as false. Lines following an \\else are processed only if no earlier matching \\if or \\elif succeeded. The expression argument of an \\if or \\elif command is subject to variable interpolation and backquote expansion, just like any other backslash command argument. After that it is evaluated like the value of an on/off option variable. So a valid value is any unambiguous case-insensitive match for one of: true , false , 1 , 0 , on , off , yes , no . For example, t , T , and tR will all be considered to be true . Expressions that do not properly evaluate to true or false will generate a warning and be treated as false. Lines being skipped are parsed normally to identify queries and backslash commands, but queries are not sent to the server, and backslash commands other than conditionals ( \\if , \\elif , \\else , \\endif ) are ignored. Conditional commands are checked only for valid nesting. Variable references in skipped lines are not expanded, and backquote expansion is not performed either. All the backslash commands of a given conditional block must appear in the same source file. If EOF is reached on the main input file or an \\include -ed file before all local \\if -blocks have been closed, then psql will raise an error. Here is an example: -- check for the existence of two separate records in the database and store -- the results in separate psql variables SELECT EXISTS (SELECT 1 FROM customer WHERE customer_id = 123) AS is_customer, EXISTS (SELECT 1 FROM employee WHERE employee_id = 456) AS is_employee \\gset \\if :is_customer SELECT * FROM customer WHERE customer_id = 123; \\elif :is_employee \\echo 'is not a customer but is an employee' SELECT * FROM employee WHERE employee_id = 456; \\else \\if yes \\echo 'not a customer or employee' \\else \\echo 'this will never print' \\endif \\endif",
   "modifiers": [],
   "signature": "\\if expression \\elif expression \\else \\endif",
   "spellings": [

Compares recorded interfaces and attributes. Source fingerprints and build metadata are excluded; an absent sample is not proof of the introduction or removal release.

Related entries

Export JSON · Back to psql Commands · Recorded in PostgreSQL 10 through 20; the first sample is not necessarily its introduction.