简体   繁体   中英

PostgreSQL INSERT INTO table that does not exist

I have some temp table:

CREATE TEMP TABLE IF NOT EXISTS temp_test (
col1 INTEGER NOT NULL,
col2 CHARACTER VARYING NOT NULL,
col3 BOOLEAN);

Then I do some inserts into temp_test (that works fine).

Later, without creating a new table test , I try doing the following:

INSERT INTO test(col1,col2,col3) SELECT col1,col2,col3 FROM temp_tes;

And I get the following error: ERROR: relation "test" does not exist

I thought that if I'm using INSERT INTO , it should create the table for me. does it not?

If it matters, I'm using PostgreSQL 9.6.16.

You are wrong. INSERT inserts into an existing table; it does not create a table.

If you want to create a table, use CREATE TABLE AS :

CREATE TABLE test AS
    SELECT col1, ol2, col3
    FROM temp_tes;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM