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
Related
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.
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.
I have a database with many tables that are used as a reference for valid values in the other tables. These reference tables are all named valid[table], so I can display them in psql with \dt valid*.
I'd like to be able to select all the non-reference tables (which are more variably named, but none of them start with valid), but can't figure out how. I've tried various things like \dt !valid* , \dt !~valid* , \dt NOT LIKE 'valid%', but these either error or don't find a match.
Any suggestions would be appreciated!
According to the manual, psql supports these patterns. Normally you could use regular expression negative lookahead (?!valid)* but ? cannot be used in regular expression with psql because it is translated to .. It is probably easier to do a query from INFROMATION SCHEMA:
SELECT table_name FROM information_schema.tables
WHERE table_schema NOT IN ('pg_catalog', 'information_schema') --exclude system tables
AND table_type = 'BASE TABLE' -- only tables
AND table_name NOT LIKE 'valid%';
I have looked into the documentation for GRANT Found here and I was trying to see if there is a built-in function that can let me look at what level of accessibility I have in databases. Of course there is:
\dp and \dp mytablename
But this does not show what my account has access to. I would like to see ALL the tables I have access to. Can anyone tell me if there is a command that can check my level of access in Postgres (whether I have SELECT, INSERT, DELETE, UPDATE privileges)? And if so, what would that command be?
You could query the table_privileges table in the information schema:
SELECT table_catalog, table_schema, table_name, privilege_type
FROM information_schema.table_privileges
WHERE grantee = 'MY_USER'
For all users on a specific database, do the following:
# psql
\c your_database
select grantee, table_catalog, privilege_type, table_schema, table_name from information_schema.table_privileges order by grantee, table_schema, table_name;
Use this to list Grantee too and remove (PG_monitor and Public) for Postgres PaaS Azure.
SELECT grantee,table_catalog, table_schema, table_name, privilege_type
FROM information_schema.table_privileges
WHERE grantee not in ('pg_monitor','PUBLIC');
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".