Where is temporary table created? - postgresql

Where can I find created temporary table in posgresql folders? If I do select * from temp_table; then I got result, but cannot see it structure of my database in the PgAdmin?

Temporary tables get put into a schema called "pg_temp_NNN", where "NNN" indicates which server backend you're connected to. This is implicitly added to your search path in the session that creates them.
Note that you can't access one connection's temp tables via another connection... so depending on how exactly pgAdmin organises its connections, even being able to find the tables in the object explorer might not be useful.

Here is one way to get the name of the pg_temp_nnn schema for your session:
select distinct 'pg_temp_'||sess_id from pg_stat_activity where procpid = pg_backend_pid()
This will identify the session that is running that SQL statement itself, and returns the session id that it is running under.
You can then use this to list all your temporary tables:
select *
from information_schema.tables
where table_schema =
( select distinct 'pg_temp_'||sess_id
from pg_stat_activity
where procpid = pg_backend_pid()
)
Or to get the table structure:
select *
from information_schema.columns
where table_schema =
( select distinct 'pg_temp_'||sess_id
from pg_stat_activity
where procpid = pg_backend_pid()
)
and table_name = 'my_temp_table'
order by ordinal_position

Related

Retrieve tables name from several databases in Postgresql?

I have two databases new_site,old_site I'm connecting to the database server via Postgres user and have full permission and I connect to new_site db.
I need to get tables names for old_site so I tried this:
SELECT table_name
FROM information_schema.tables
WHERE table_catalog = $$old_site$$;
but I get a null as result.
If I run this query:
SELECT table_name
FROM information_schema.tables
WHERE table_catalog = current_database();
I get back the table name and it works.
I expect the output is table name of old_site db, how can I do this?
I was also reading some solutions here like:
Selecting column name from other database table through function in PostgreSQL
But it's not like my case.

Do Postgres temporary tables exist between multiple connections open at once?

Say I open a new npgsqlconnection and create a new temporary table temp1, and then open another new connection. From my understanding a temporary table is only available to the session that opened it, and two open connections shouldn't share the same session. Here the connection strings are identical, and I tried turning pooling off, but that didn't change anything. The pseudo-code is:
var conn1 = new NpgsqlConnection(MyConnectionString)
var conn2 = new NpgsqlConnection(MyConnectionString)
conn1.Open()
conn2.Open()
conn1.Execute("CREATE TEMP TABLE temp1(idx int)")
If I execute the query SELECT COUNT(*) FROM pg_tables WHERE tablename = 'temp1' for both connections this query returns 1. Why would conn2 be able to access the temporary table created on conn1? Is there anyway to prevent this?
Why would conn2 be able to access the temporary table created on conn1?
It can't.
The other connections can see that there is a table via the system catalog, but they cannot access it.
-- Connection 1
test=# SELECT schemaname FROM pg_tables WHERE tablename = 'temp1';
schemaname
------------
pg_temp_3
(1 row)
-- Connection 2
test=# select * from pg_temp_3.temp1;
ERROR: cannot access temporary tables of other sessions

Phantom Postgres table exists but can't be dropped?

I seem to have some sort of phantom table in Postgres.
Suppose I do the following:
select * from information_schema.tables where table_schema = 'public';
I get:
table_name | table_type | ...
phantom_table BASE TABLE
...
So, I run:
drop table phantom_table cascade;
And I get:
ERROR: table "phantom_table" does not exist
Things I've tried:
Checking for spelling errors and making sure the schema is correct (I've even copied/pasted table name out of information schema query results).
vacuum
Reconnecting.
Killing other running processes from my user (nobody else is using the DB).
Checking for active locks on the table (there aren't any).
Anybody have any other ideas for things I should try?
You probably have some white space at the end of the name.
The easiest way is to let the format() function generate you the correct table name and statement:
select format('drop table %I.%I;', table_schema, table_name) as drop_statement
from information_schema.tables
where table_schema = 'public'
and table_name like '%phantom%';
Edit: it seems that psql on Windows isn't able to handle an identifier with a new line in a drop statement (it does when creating the table however).
To workaround that, you can use a DO block:
do
$$
declare
l_stmt text;
begin
select format('drop table %I.%I;', table_schema, table_name) as drop_statement
into l_stmt
from information_schema.tables
where table_schema = 'public'
and table_name like '%phantom%';
execute l_stmt;
end;
$$
;
Note this code assumes that only a single table with that name exists.

postgresql error when attempting to do a select all from table

I am trying to do the postgresql equivalent of mysql select * from table on a postgresql specific database. I can find the name of the table I need within that database when I do:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
But when I try a select all on the table, I get:
SELECT * from Sample;
SELECT * from Sample;
ERROR: relation "sample" does not exist
LINE 1: SELECT * from Sample;
^
Any ideas?
Postgresql is case sensitive.
I usually use all lower char for field, tables and functions.
Anyway, you can double quote them.
To full answer your question and see why and when useing quote, i suggest to read this specific section:
http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
The manes of the tables are stored in information_schema.tables so you have to see your table by using select * from information_schema.tables. IF your table_schema is "public"
try for a table select select * from public.sample, is your table_schema a differed schema chance it to the right one.
this link will help you Psotgresql doc
I tried single-quoting 'Sample' and it didn't work. Fixed by double-quoting "Sample".

PostgreSQL query to list all table names?

Is there any query available to list all tables in my Postgres DB.
I tried out one query like:
SELECT table_name FROM information_schema.tables
WHERE table_schema='public'
But this query returns views also.
How can i get only table names only, not views?
What bout this query (based on the description from manual)?
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public'
AND table_type='BASE TABLE';
If you want list of database
SELECT datname FROM pg_database WHERE datistemplate = false;
If you want list of tables from current pg installation of all databases
SELECT table_schema,table_name FROM information_schema.tables
ORDER BY table_schema,table_name;
Open up the postgres terminal with the databse you would like:
psql dbname (run this line in a terminal)
then, run this command in the postgres environment
\d
This will describe all tables by name. Basically a list of tables by name ascending.
Then you can try this to describe a table by fields:
\d tablename.
Hope this helps.
Try this:
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public' AND table_type='BASE TABLE'
this one works!
SELECT table_name
FROM information_schema.tables
WHERE table_type='BASE TABLE'
AND table_schema='public';
For MySQL you would need table_schema='dbName' and for MSSQL remove that condition.
Notice that "only those tables and views are shown that the current user has access to". Also, if you have access to many databases and want to limit the result to a certain database, you can achieve that by adding condition AND table_catalog='yourDatabase' (in PostgreSQL).
If you'd also like to get rid of the header showing row names and footer showing row count, you could either start the psql with command line option -t (short for --tuples-only) or you can toggle the setting in psql's command line by \t (short for \pset tuples_only). This could be useful for example when piping output to another command with \g [ |command ].
How about giving just \dt in psql? See https://www.postgresql.org/docs/current/static/app-psql.html.
select
relname as table
from
pg_stat_user_tables
where schemaname = 'public'
This will not work if track_activities is disabled
select
tablename as table
from
pg_tables
where schemaname = 'public'
Read more about pg_tables