SELECT_INTO(7pg18)
๐ SELECT INTO (7) โ bestie edition ๐
okay so this is the PostgreSQL 18.6 docs page for SELECT INTO, section 7 of the manual (iykyk), and we're gonna get you fluent in it fr fr. grab a snack, this one's short but it has LORE. ๐
โจ NAME โจ
SELECT_INTO โ define a new table from the results of a query
like... you run a query and the results become a whole new table?? main character energy honestly ๐
๐ธ SYNOPSIS ๐ธ
[ WITH [ RECURSIVE ] with_query [, ...] ]
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
[ { * | expression [ [ AS ] output_name ] } [, ...] ]
INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table
[ FROM from_item [, ...] ]
[ WHERE condition ]
[ GROUP BY expression [, ...] ]
[ HAVING condition ]
[ WINDOW window_name AS ( window_definition ) [, ...] ]
[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ LIMIT { count | ALL } ]
[ OFFSET start [ ROW | ROWS ] ]
[ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
[ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]
no cap, that's the whole shape of it. it's giving "SELECT but make it a table" ๐
๐ฆ DESCRIPTION ๐ฆ
SELECT INTO creates a new table and fills it with data computed by a query. the tea: the data does not get returned to the client, unlike a normal SELECT. it just... quietly becomes a table. iconic behavior honestly.
the new table's columns get the names and data types associated with the output columns of the SELECT. so whatever your query spits out, that's your table's whole personality. โจ
๐ PARAMETERS ๐
TEMPORARYorTEMPโ if specified, the table is created as a temporary table. refer toCREATE TABLE(CREATE_TABLE(7)) for details. ๐ซงUNLOGGEDโ if specified, the table is created as an unlogged table. refer toCREATE TABLE(CREATE_TABLE(7)) for details. (yes bestie, same page, go read it)new_tableโ the name (optionally schema-qualified) of the table to be created. name her well ๐
all other parameters are described in detail under SELECT(7). like, every single one, we're not redoing that here.
๐ NOTES ๐
CREATE TABLE AS is functionally similar to SELECT INTO. and CREATE TABLE AS is the recommended syntax, because this form of SELECT INTO is not available in ECPG or PL/pgSQL โ they interpret the INTO clause differently. ๐ญ plot twist. furthermore, CREATE TABLE AS offers a superset of the functionality provided by SELECT INTO. superset!! she ate and left no crumbs.
in contrast to CREATE TABLE AS, SELECT INTO does not allow specifying properties like a table's access method with USING method or the table's tablespace with TABLESPACE tablespace_name. use CREATE TABLE AS if necessary. therefore, the default table access method is chosen for the new table. see default_table_access_method for more information. ๐
so like. it works. it's fine. but CREATE TABLE AS is the moment. just saying. ๐
๐ EXAMPLES ๐
create a new table films_recent consisting of only recent entries from the table films:
SELECT * INTO films_recent FROM films WHERE date_prod >= '2002-01-01';
okay look at her go. one line, whole new table, slay. ๐ฌโจ
๐ง COMPATIBILITY ๐ง
the SQL standard uses SELECT INTO to represent selecting values into scalar variables of a host program, rather than creating a new table. and that's genuinely the usage found in ECPG (see Chapter 34) and PL/pgSQL (see Chapter 41). so if you came from those worlds you're not wrong, you're just in a different room bestie.
the PostgreSQL usage of SELECT INTO to represent table creation is historical. some other SQL implementations also use SELECT INTO in this way (but most SQL implementations support CREATE TABLE AS instead). apart from such compatibility considerations, it is best to use CREATE TABLE AS for this purpose in new code. ๐ซถ
lowkey the whole compatibility section is just the docs gently telling you "please use CREATE TABLE AS" three separate times. they're being so nice about it and yet. ๐
๐ SEE ALSO ๐
CREATE TABLE AS(CREATE_TABLE_AS(7))
and that's the page!! you're basically a SELECT INTO expert now, no cap. ๐โจ
okay bye, this was PostgreSQL 18.6, 2026, SELECT INTO(7) โ signing off, mwah ๐
๐ธ๐ฆ
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms