PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, when you create a temporary table, you provide a name for it, just as you would for a regular table. The name you provide for the temporary table is the one you'll use to reference it within your session.
Here's a basic outline of naming and referencing temporary tables:
Creation:
Create a temporary table with a name you choose:
CREATE TEMP TABLE my_temp_table (id SERIAL, name TEXT);
In this case, the temporary table's name is my_temp_table
.
Reference:
After creation, you reference the temporary table by the name you've given it:
INSERT INTO my_temp_table (name) VALUES ('John Doe'); SELECT * FROM my_temp_table;
Naming Conflicts:
Schema:
Temporary tables are usually created in a special schema, so you don't need to worry about naming conflicts with regular tables across different schemas. By default, PostgreSQL creates temporary tables in a schema named pg_temp
, but you typically don't need to specify the schema when referencing the table in your queries.
Uniqueness Within Session:
Within a single session, the temporary table name must be unique. You cannot create two temporary tables with the same name in the same session.
Visibility Across Sessions:
Temporary tables are session-specific. This means two different sessions can create temporary tables with the same name without any conflicts because each session can't see the other's temporary tables.
In summary, when naming a temporary table in PostgreSQL, you provide a name and use that to reference the table throughout your session. Be mindful of naming conflicts with permanent tables, although the temporary table will take precedence within your session. Also, remember that other sessions can't see or access your temporary tables, regardless of the name.
How to generate a dynamic name for a temporary table in PostgreSQL: You can generate a dynamic name using a combination of a base name and a unique identifier:
CREATE TEMPORARY TABLE temp_table_ || random() AS ( -- Your table definition here );
Dynamic temporary table name in PostgreSQL query: Dynamically generate a temporary table name within a query using a unique identifier:
CREATE TEMPORARY TABLE 'temp_table_' || random() AS ( -- Your table definition here );
Random temporary table name in PostgreSQL:
Utilize the random()
function to generate a random part for the temporary table name:
CREATE TEMPORARY TABLE 'temp_table_' || random() AS ( -- Your table definition here );
Concatenate string for temporary table name in PostgreSQL: Concatenate strings to create a dynamic temporary table name:
CREATE TEMPORARY TABLE 'temp_table_' || variable_name AS ( -- Your table definition here );
Using variables for temporary table name in PostgreSQL: Use a variable to store the dynamic part of the temporary table name:
DO $$ DECLARE table_name text := 'temp_table_' || random(); BEGIN EXECUTE 'CREATE TEMPORARY TABLE ' || table_name || ' AS ( -- Your table definition here )'; END $$;
Create temporary table with timestamp in name PostgreSQL: Include a timestamp in the temporary table name for uniqueness:
CREATE TEMPORARY TABLE 'temp_table_' || to_char(now(), 'YYYYMMDDHH24MISS') AS ( -- Your table definition here );
Dynamic schema and temporary table name in PostgreSQL: Combine dynamic schema and temporary table name:
CREATE TEMPORARY TABLE schema_name.'temp_table_' || random() AS ( -- Your table definition here );