The output shows the schema of the customers temporary table is pg_temp_3. Public | customers_id_seq | sequence | postgres If you list the tables in the test database, you will only see the temporary table customers, not the permanent one: List of relations Note that PostgreSQL creates temporary tables in a special schema, therefore, you cannot specify the schema in the CREATE TEMP TABLE statement. This time PostgreSQL accessed the temporary table customers instead of the permanent one. Now, query data from the customers table: SELECT * FROM customers Second, create a temporary table with the same name: customers CREATE TEMP TABLE customers( Consider the following example:įirst, create a table named customers: CREATE TABLE customers( When you create a temporary table that shares the same name as a permanent table, you cannot access the permanent table until the temporary table is removed. PostgreSQL temporary table nameĪ temporary table can have the same name as a permanent table, even though it is not recommended. The mytemp table does not exist because it has been dropped automatically when the session ended, therefore, PostgreSQL issued an error. Only the first session can access it.Īfter that, quit all the sessions: test=# \q Code language: PostgreSQL SQL dialect and PL/pgSQL ( pgsql )įinally, log in to the database server again and query data from the mytemp table: test=# SELECT * FROM mytemp Then, launch another session that connects to the test database and query data from the mytemp table: test=# SELECT * FROM mytemp Īs can see clearly from the output, the second session could not see the mytemp table. Next, create a temporary table named mytemp as follows: test=# CREATE TEMP TABLE mytemp(c INT) Code language: PostgreSQL SQL dialect and PL/pgSQL ( pgsql ) You are now connected to database "test" as user "postgres". In other words, it is invisible to other sessions.įirst, log in to the PostgreSQL database server using the psql program and create a new database named test: postgres=# CREATE DATABASE test The TEMP and TEMPORARY keywords are equivalent so you can use them interchangeably: CREATE TEMP TABLE temp_table(Ĭode language: PostgreSQL SQL dialect and PL/pgSQL ( pgsql )Ī temporary table is visible only to the session that creates it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |