Wiki / psql Commands / Query execution
\gset
Sends the current query buffer to the server and stores the query's output into psql variables (see Variables below). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as …
Reading PostgreSQL 18.6.
Description
Sends the current query buffer to the server and stores the query's output into psql variables (see Variables below). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example:
- Client
- psql 18.6
- Case-sensitive spelling
- \gset
Usage
\gset [ prefix ]Spellings in this manual definition
| Command | Documented suffix modifiers |
|---|---|
| \gset | None listed in this signature |
Manual definition
\gset [prefix]-
Sends the current query buffer to the server and stores the query's output into psql variables (see Variables below). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example:
=>
SELECT 'hello' AS var1, 10 AS var2->\gset=>\echo :var1 :var2hello 10If you specify a
prefix, that string is prepended to the query's column names to create the variable names to use:=>
SELECT 'hello' AS var1, 10 AS var2->\gset result_=>\echo :result_var1 :result_var2hello 10If a column result is NULL, the corresponding variable is unset rather than being set.
If the query fails or does not return one row, no variables are changed.
If the current query buffer is empty, the most recently sent query is re-executed instead.
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 12 → 13: changed.
--- PostgreSQL 12
+++ PostgreSQL 13
@@ -1,5 +1,5 @@
{
- "definition": "Sends the current query buffer to the server and stores the query's output into psql variables (see Variables ). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example: => SELECT 'hello' AS var1, 10 AS var2 -> \\gset => \\echo :var1 :var2 hello 10 If you specify a prefix , that string is prepended to the query's column names to create the variable names to use: => SELECT 'hello' AS var1, 10 AS var2 -> \\gset result_ => \\echo :result_var1 :result_var2 hello 10 If a column result is NULL, the corresponding variable is unset rather than being set. If the query fails or does not return one row, no variables are changed. If the current query buffer is empty, the most recently sent query is re-executed instead.",
+ "definition": "Sends the current query buffer to the server and stores the query's output into psql variables (see Variables below). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example: => SELECT 'hello' AS var1, 10 AS var2 -> \\gset => \\echo :var1 :var2 hello 10 If you specify a prefix , that string is prepended to the query's column names to create the variable names to use: => SELECT 'hello' AS var1, 10 AS var2 -> \\gset result_ => \\echo :result_var1 :result_var2 hello 10 If a column result is NULL, the corresponding variable is unset rather than being set. If the query fails or does not return one row, no variables are changed. If the current query buffer is empty, the most recently sent query is re-executed instead.",
"modifiers": [],
"signature": "\\gset [ prefix ]",
"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.