186

In MS SQL Server, I create my scripts to use customizable variables:

DECLARE @somevariable int  
SELECT @somevariable = -1

INSERT INTO foo VALUES ( @somevariable )

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.

How do I do the same with the PostgreSQL client psql?

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
Craig Walker
  • 49,871
  • 54
  • 152
  • 212

13 Answers13

233

Postgres variables are created through the \set command, for example ...

\set myvariable value

... and can then be substituted, for example, as ...

SELECT * FROM :myvariable.table1;

... or ...

SELECT * FROM table1 WHERE :myvariable IS NULL;

edit: As of psql 9.1, variables can be expanded in quotes as in:

\set myvariable value 

SELECT * FROM table1 WHERE column1 = :'myvariable';

In older versions of the psql client:

... If you want to use the variable as the value in a conditional string query, such as ...

SELECT * FROM table1 WHERE column1 = ':myvariable';

... then you need to include the quotes in the variable itself as the above will not work. Instead define your variable as such ...

\set myvariable 'value'

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 ...

\set quoted_myvariable '\'' :myvariable '\''

Now you have both a quoted and unquoted variable of the same string! And you can do something like this ....

INSERT INTO :myvariable.table1 SELECT * FROM table2 WHERE column1 = :quoted_myvariable;
Jeremy
  • 1,198
  • 1
  • 9
  • 16
crowmagnumb
  • 6,621
  • 9
  • 33
  • 42
  • 92
    `\set` is only for `psql` tool, you cannot use it in stored procedures! – sorin Apr 11 '12 at 09:48
  • 9
    @SorinSbarnea the OP asked about *script*, not *procedure* – Daniel Serodio Jul 17 '12 at 18:23
  • 57
    This answer mixes `psql` meta-commands `\set` with PostgreSQL commands in a confusing fashion. – Erwin Brandstetter Jan 10 '13 at 15:40
  • 22
    As of postgresql 9.1, in psql you can now use :'variable' to have it properly quoted as a value for you, or :"variable" to use it as an identifier. – HitScan Oct 29 '13 at 14:21
  • The second one `\set quoted_myvariable '\'' :myvariable '\''` gives you the quotes in the variable. – crowmagnumb May 07 '14 at 21:26
  • 1
    \set works like a C macro. You can't use it to store the result of a function, like RANDOM() or NOW(). – geon Oct 27 '14 at 12:59
  • 2
    `\set myvar 'value'` does not work the way you describe. You have to use `\set myvar '\'value\''` – laurent Apr 24 '17 at 10:36
  • instead of quoting, you can use a cast too: \set myvariable abcd, then select col from table where attr = :myvariable::varchar, for instance – PW. Oct 20 '17 at 07:21
  • 3
    @laurent: Or use `:'myvar'` like @HitScan suggests – Andomar Jan 30 '18 at 10:39
  • It is a bad practice to provide non working code: ':myvariable'. I see code and don't read comments that it is wrong code. So lost some time to detect bug and revoke my script from collegues. A way better is just provide correct code. – Dzenly Mar 16 '18 at 07:20
  • quoting with `$$` also works - `\set quoted_myvariable $$':myvariable'$$` – srghma Nov 17 '18 at 09:22
  • also variant with arrays: 1. `\set args $$'{"text", "text", "text", "text"}'$$::text[]` is same as `\set args ARRAY[$$'text'$$, $$'text'$$, $$'text'$$, $$'text'$$]` – srghma Nov 18 '18 at 13:16
73

One final word on PSQL variables:

  1. They don't expand if you enclose them in single quotes in the SQL statement. Thus this doesn't work:

    SELECT * FROM foo WHERE bar = ':myvariable'
    
  2. To expand to a string literal in a SQL statement, you have to include the quotes in the variable set. However, the variable value already has to be enclosed in quotes, which means that you need a second set of quotes, and the inner set has to be escaped. Thus you need:

    \set myvariable '\'somestring\''  
    SELECT * FROM foo WHERE bar = :myvariable
    

    EDIT: starting with PostgreSQL 9.1, you may write instead:

    \set myvariable somestring
    SELECT * FROM foo WHERE bar = :'myvariable'
    
Daniel Vérité
  • 58,074
  • 15
  • 129
  • 156
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
70

You can try to use a WITH clause.

WITH vars AS (SELECT 42 AS answer, 3.14 AS appr_pi)
SELECT t.*, vars.answer, t.radius*vars.appr_pi
FROM table AS t, vars;
skaurus
  • 1,581
  • 17
  • 27
  • 1
    This way is mostly convenient when you are using same computed values few times in your query. – skaurus Mar 08 '13 at 14:36
  • 2
    Contrary to Bryce's report, it seems to work fine for me. `CREATE TABLE test (name VARCHAR, age INT);` `INSERT INTO test (name, age) VALUES ('Jack', 21), ('Jill', 20);` `WITH vars AS (SELECT N'Jack' AS name, 21 AS age) SELECT test.* FROM test, vars WHERE test.name = vars.name and test.age = vars.age;` Ouputs Jack and Jack's age, as expected. – Joshua Sep 22 '14 at 20:01
  • 2
    For a lot of uses, especially within the context of a web application framework like Python Flask, this is the best solution for reusing complex calculated values within a single query. – Will Oct 21 '15 at 17:07
  • 1
    Can anyone suggest how this might work in an insert? – Stoopkid Apr 11 '17 at 00:39
  • @Stoopkid `create table t(x integer);` `insert into t(x) with sub as (select 999 as num) select num from sub;` `select * from t;` – JL_SO Jan 04 '19 at 14:56
  • with vars as (select 42 as uid) select id from users where id = vars.uid returns error: ERROR: missing FROM-clause entry for table "vars" – Artem Mar 29 '23 at 06:50
  • 1
    @Artem try adding vars to the FROM clause: `... from users, vars where ...` – skaurus Mar 30 '23 at 09:39
47

Specifically for psql, you can pass psql variables from the command line too; you can pass them with -v. Here's a usage example:

$ psql -v filepath=/path/to/my/directory/mydatafile.data regress
regress=> SELECT :'filepath';
               ?column?                
---------------------------------------
 /path/to/my/directory/mydatafile.data
(1 row)

Note that the colon is unquoted, then the variable name its self is quoted. Odd syntax, I know. This only works in psql but doesn't work with -c / --command; you have to send the command via stdin or via -f. It won't work in (say) PgAdmin-III too.

This substitution happens during input processing in psql, so you can't (say) define a function that uses :'filepath' and expect the value of :'filepath' to change from session to session. It'll be substituted once, when the function is defined, and then will be a constant after that. It's useful for scripting but not runtime use.

kirikaza
  • 2,659
  • 2
  • 15
  • 14
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • 1
    psql variables, e.g. :'filepath', you pointed out: "Note that the colon is unquoted, then the variable name its self is quoted." Thank! You! I already put a bunch of forehead-shaped dents into my desk trying to make this work, and you just saved me a ton more. Exactly what I needed for some scripting. – Jason Mar 20 '17 at 03:34
  • This doesn't seem to work with `--command="SELECT :'filepath';"`, which is unfortunate. How can this be useful if it seems to require interactive terminal sessions? – alphabetasoup Mar 10 '23 at 02:47
  • However `echo "\set filepath /path/to/my/directory/mydatafile.data \\\\\\\\ SELECT :'filepath';" | psql` does work. Note the excessive number of escaping slashes to achieve two sequential \ characters, as per the advice under the `command` arg [here](https://www.postgresql.org/docs/current/app-psql.html), with a Bash shell anyway. – alphabetasoup Mar 10 '23 at 03:01
  • @alphabetasoup Yep, you can use `psql -f -` etc. Like many things, `psql` isn't exactly pretty, but it gets the job done. It's easier to use `-v` than to compose a `\set` command in stdin though. – Craig Ringer May 22 '23 at 03:38
15

FWIW, the real problem was that I had included a semicolon at the end of my \set command:

\set owner_password 'thepassword';

The semicolon was interpreted as an actual character in the variable:

\echo :owner_password thepassword;

So when I tried to use it:

CREATE ROLE myrole LOGIN UNENCRYPTED PASSWORD :owner_password NOINHERIT CREATEDB CREATEROLE VALID UNTIL 'infinity';

...I got this:

CREATE ROLE myrole LOGIN UNENCRYPTED PASSWORD thepassword; NOINHERIT CREATEDB CREATEROLE VALID UNTIL 'infinity';

That not only failed to set the quotes around the literal, but split the command into 2 parts (the second of which was invalid as it started with "NOINHERIT").

The moral of this story: PostgreSQL "variables" are really macros used in text expansion, not true values. I'm sure that comes in handy, but it's tricky at first.

Craig Walker
  • 49,871
  • 54
  • 152
  • 212
13

postgres (since version 9.0) allows anonymous blocks in any of the supported server-side scripting languages

DO '
DECLARE somevariable int = -1;
BEGIN
INSERT INTO foo VALUES ( somevariable );
END
' ;

http://www.postgresql.org/docs/current/static/sql-do.html

As everything is inside a string, external string variables being substituted in will need to be escaped and quoted twice. Using dollar quoting instead will not give full protection against SQL injection.

Jasen
  • 11,837
  • 2
  • 30
  • 48
11

You need to use one of the procedural languages such as PL/pgSQL not the SQL proc language. In PL/pgSQL you can use vars right in SQL statements. For single quotes you can use the quote literal function.

  • 5
    It cannot be done in postgres itself, but it can be done in the PSQL client application. – Philluminati Jul 27 '11 at 11:27
  • 1
    plpgsql can (now) be used in postgres (since version 9.0) )http://www.postgresql.org/docs/9.0/static/sql-do.html – Jasen May 10 '16 at 22:51
6

I solved it with a temp table.

CREATE TEMP TABLE temp_session_variables (
    "sessionSalt" TEXT
);
INSERT INTO temp_session_variables ("sessionSalt") VALUES (current_timestamp || RANDOM()::TEXT);

This way, I had a "variable" I could use over multiple queries, that is unique for the session. I needed it to generate unique "usernames" while still not having collisions if importing users with the same user name.

geon
  • 8,128
  • 3
  • 34
  • 41
5

Another approach is to (ab)use the PostgreSQL GUC mechanism to create variables. See this prior answer for details and examples.

You declare the GUC in postgresql.conf, then change its value at runtime with SET commands and get its value with current_setting(...).

I don't recommend this for general use, but it could be useful in narrow cases like the one mentioned in the linked question, where the poster wanted a way to provide the application-level username to triggers and functions.

Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
3

I really miss that feature. Only way to achieve something similar is to use functions.

I have used it in two ways:

  • perl functions that use $_SHARED variable
  • store your variables in table

Perl version:

   CREATE FUNCTION var(name text, val text) RETURNS void AS $$
        $_SHARED{$_[0]} = $_[1];
   $$ LANGUAGE plperl;
   CREATE FUNCTION var(name text) RETURNS text AS $$
        return $_SHARED{$_[0]};
   $$ LANGUAGE plperl;

Table version:

CREATE TABLE var (
  sess bigint NOT NULL,
  key varchar NOT NULL,
  val varchar,
  CONSTRAINT var_pkey PRIMARY KEY (sess, key)
);
CREATE FUNCTION var(key varchar, val anyelement) RETURNS void AS $$
  DELETE FROM var WHERE sess = pg_backend_pid() AND key = $1;
  INSERT INTO var (sess, key, val) VALUES (sessid(), $1, $2::varchar);
$$ LANGUAGE 'sql';

CREATE FUNCTION var(varname varchar) RETURNS varchar AS $$
  SELECT val FROM var WHERE sess = pg_backend_pid() AND key = $1;
$$ LANGUAGE 'sql';

Notes:

  • plperlu is faster than perl
  • pg_backend_pid is not best session identification, consider using pid combined with backend_start from pg_stat_activity
  • this table version is also bad because you have to clear this is up occasionally (and not delete currently working session variables)
Kaiko Kaur
  • 341
  • 3
  • 11
3

I've found this question and the answers extremely useful, but also confusing. I had lots of trouble getting quoted variables to work, so here is the way I got it working:

\set deployment_user username    -- username
\set deployment_pass '\'string_password\''
ALTER USER :deployment_user WITH PASSWORD :deployment_pass;

This way you can define the variable in one statement. When you use it, single quotes will be embedded into the variable.

NOTE! When I put a comment after the quoted variable it got sucked in as part of the variable when I tried some of the methods in other answers. That was really screwing me up for a while. With this method comments appear to be treated as you'd expect.

Nate
  • 2,940
  • 3
  • 22
  • 24
  • `\set deployment_pass 'string_password'` `ALTER USER :deployment_user WITH PASSWORD :'deployment_pass';` – Jasen May 10 '16 at 23:23
  • \set is not SQL it's a psql built-in command sql comments are not supported. – Jasen May 10 '16 at 23:25
2

Variables in psql suck. If you want to declare an integer, you have to enter the integer, then do a carriage return, then end the statement in a semicolon. Observe:

Let's say I want to declare an integer variable my_var and insert it into a table test:

Example table test:

thedatabase=# \d test;
                         Table "public.test"
 Column |  Type   |                     Modifiers                     
--------+---------+---------------------------------------------------
 id     | integer | not null default nextval('test_id_seq'::regclass)
Indexes:
    "test_pkey" PRIMARY KEY, btree (id)

Clearly, nothing in this table yet:

thedatabase=# select * from test;
 id 
----
(0 rows)

We declare a variable. Notice how the semicolon is on the next line!

thedatabase=# \set my_var 999
thedatabase=# ;

Now we can insert. We have to use this weird ":''" looking syntax:

thedatabase=# insert into test(id) values (:'my_var');
INSERT 0 1

It worked!

thedatabase=# select * from test;
 id  
-----
 999
(1 row)

Explanation:

So... what happens if we don't have the semicolon on the next line? The variable? Have a look:

We declare my_var without the new line.

thedatabase=# \set my_var 999;

Let's select my_var.

thedatabase=# select :'my_var';
 ?column? 
----------
 999;
(1 row)

WTF is that? It's not an integer, it's a string 999;!

thedatabase=# select 999;
 ?column? 
----------
      999
(1 row)
Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
  • 12
    The reason the semicolon does unexpected things for you is that a semicolon terminates a SQL statement, but you're typing a psql command, \set, which isn't SQL and does NOT take a terminating semicolon. Putting a semicolon on the next line won't hurt, but does absolutely nothing. It's an empty statement. – volkerk Apr 15 '19 at 22:26
  • 1
    Also, you shouldn't use the `:'my_var'` syntax for an integer value. `:my_var` works just fine. – cstork Mar 17 '21 at 08:47
2

I've posted a new solution for this on another thread.

It uses a table to store variables, and can be updated at any time. A static immutable getter function is dynamically created (by another function), triggered by update to your table. You get nice table storage, plus the blazing fast speeds of an immutable getter.

Brev
  • 101
  • 1
  • 3