I am learning PostgreSQL and trying to figure out how to create a temporary table or a WITH declaration that can be used in place of regular table, for debugging purposes.

I looked at the documentation for CREATE TABLE and it says VALUES can be used as a query but gives no example; the documentation for the VALUES clause linked therein does not have an example either?

So, I wrote a simple test as follows:

DROP TABLE IF EXISTS lookup;
CREATE TEMP TABLE lookup (
  key integer,
  val numeric
) AS
VALUES (0,-99999), (1,100);

But PostgreSQL (9.3) is complaining about

syntax error at or near "AS"

My questions are:

  1. How can I fix the statement above?

  2. How can I adapt it to be used in a WITH block?

Thanks in advance.

up vote 28 down vote accepted

If you just want to select from some values, rather than just creating a table and inserting into it, you can do something like:

WITH  temp (k,v) AS (VALUES (0,-9999), (1, 100)) 
SELECT * FROM temp;

To actually create a temporary table in a similar fashion, use:

WITH  vals (k,v) AS (VALUES (0,-9999), (1, 100)) 
SELECT * INTO temporary table  temp FROM vals;
  • 10
    from the docs: "CREATE TABLE AS is functionally similar to SELECT INTO. CREATE TABLE AS is the recommended syntax" – a_horse_with_no_name Dec 21 '14 at 23:14

create table as needs a select statement:

DROP TABLE IF EXISTS lookup;
CREATE TEMP TABLE lookup 
as 
select *
from (
   VALUES 
    (0::int,-99999::numeric), 
    (1::int, 100::numeric)
) as t (key, value);

You can also re-write this to use a CTE:

create temp table lookup 
as 
with t (key, value) as (
  values 
    (0::int,-99999::numeric), 
    (1::int,100::numeric)
)
select * from t;

The issue is the datatypes. If you remove them, the statement will work:

CREATE TEMP TABLE lookup
  (key, val) AS
VALUES 
  (0, -99999), 
  (1, 100) ;

You can define the types by casting the values of the first row:

CREATE TEMP TABLE lookup 
  (key, val) AS
VALUES 
  (0::bigint, -99999::int), 
  (1, 100) ;

You really don't need to create a table nor use a CTE, if all you need is to use a few values in your queries. You can inline them:

SELECT  *
FROM    (VALUES(0::INT, -99999::NUMERIC), (1, 100)) AS lookup(key, val)

Then you can get a Cartesian product with a CROSS JOIN (where the other relationship can be, of course, a regular table, view, etc.). e.g.:

SELECT  *
FROM    (VALUES(0::int, -99999::numeric), (1, 100)) AS lookup(key, val)
       ,(VALUES('Red'), ('White'), ('Blue')) AS colors(color);

which yields:

key |val    |color |
----|-------|------|
0   |-99999 |Red   |
1   |100    |Red   |
0   |-99999 |White |
1   |100    |White |
0   |-99999 |Blue  |
1   |100    |Blue  |

Or JOIN the values with another relationship (which again can be a regular table, view, etc.), e.g.:

SELECT  *
FROM    (VALUES(0::int, -99999::numeric), (1, 100)) AS lookup(key, val)
  JOIN  (VALUES('Red', 1), ('White', 0), ('Blue', 1)) AS colors(color, lookup_key)
          ON colors.lookup_key = lookup.key;

which yields:

key |val    |color |lookup_key |
----|-------|------|-----------|
1   |100    |Red   |1          |
0   |-99999 |White |0          |
1   |100    |Blue  |1          |
  • OK but the question was "how to create a temporary table with...?" – ypercubeᵀᴹ Oct 20 '17 at 17:51
  • Yes, but why would you need a temporary table with a few fixed lookup values if not for joining it on another relationship? This solution solves the problem itself, regardless of how the question is worded. – isapir Oct 20 '17 at 18:37
  • 1
    Maybe OP just happened to boil down the example to something that would be easy to post as a question, but the real data has thousands of values? – stannius Jan 25 at 20:56

First always use the standardized CREATE TABLE AS, SELECT INTO as suggested in other answers has been a deprecated syntax for over a decade. You can use CREATE TABLE AS with a CTE

While many answers here are suggesting using a CTE, that's not preferable. In fact, it's likely somewhat slower. Just wrap it up as a table.

DROP TABLE IF EXISTS lookup;

CREATE TEMP TABLE lookup(key, value) AS
  VALUES
  (0::int,-99999::numeric),
  (1,100);

If you must write a select statement you can do that too (and you don't need a CTE).

CREATE TEMP TABLE lookup(key, value) AS
  SELECT key::int, value::numeric
  FROM ( VALUES
    (0::int,-99999::numeric),
    (1,100)
  ) AS t(key, value);

A CTE in PostgreSQL forces materialization. It's an optimization fence. For that reason, it's generally not a good idea to use them anywhere except when you understand the costs and you know it to provide a performance improvement. You can see the slow down here, for instance,

\timing
CREATE TABLE foo AS
  SELECT * FROM generate_series(1,1e7);
Time: 5699.070 ms

CREATE TABLE foo AS
  WITH t AS ( SELECT * FROM generate_series(1,1e7) ) 
  SELECT * FROM t;
Time: 6484.516 ms
WITH u AS (
    SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS account (id,name)
)
SELECT id, name, length(name) from u;

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.