In addition to the command line \d+ <table_name> you already found, you could also use the Information Schema to look up the column data, using information_schema.columns:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'
;
Note: As per the example above, make sure the values are enclosed within quotes.
Answer from bhamby on Stack ExchangeIn addition to the command line \d+ <table_name> you already found, you could also use the Information Schema to look up the column data, using information_schema.columns:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'
;
Note: As per the example above, make sure the values are enclosed within quotes.
As a supplement to the other answers, even a SELECT statement that returns no rows will expose the column names to you and to application code.
select *
from table_name
where false;
I intend this to be executable by almost any client software for almost any DBMS. (Almost? Yes, some don't support a clause like where false. Instead, they require an expression like where 1 = 0.)
Permissions might come into play with any of these approaches.
Show table structure and list of tables in PostgreSQL - Stack Overflow
python - PostgreSQL - query all tables' all table columns - Stack Overflow
Way to search all tables in a database for a specific column name?
How to get a list column names and data-type of a table in PostgreSQL?
Does Coefficient work with both Google Sheets and Excel?
Can I export data back to my business systems with Coefficient?
How much does Coefficient cost?
SHOW TABLES and DESCRIBE TABLE are MySQL-specific admin commands, and nothing to do with standard SQL.
You want the:
\d
and
\d+ tablename
commands from psql.
These are implemented client-side. I find this odd myself, and would love to move them server-side as built-in SQL commands one day.
Other clients provide other ways to browse the structure - for example, PgAdmin-III.
If you want a portable way to get table structure in code, you should use the information_schema views, which are SQL-standard. See information_schema. They're available in MySQL, PostgreSQL, Ms-SQL, and most other DBs. The downside is that they're fiddlier to use, so they aren't convenient for quick access when you're just browsing a DB structure.
As per the Documentation
SELECT
table_schema || '.' || table_name as show_tables
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema NOT IN ('pg_catalog', 'information_schema');
for more convenience make it as a function
create or replace function show_tables() returns SETOF text as $$
SELECT
table_schema || '.' || table_name as show_tables
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema NOT IN ('pg_catalog', 'information_schema');
$$
language sql;
So we can get the tables using
select show_tables()
For the table description
select column_name, data_type, character_maximum_length
from INFORMATION_SCHEMA.COLUMNS where table_name ='table_name';
as a Function
create or replace function describe_table(tbl_name text) returns table(column_name
varchar, data_type varchar,character_maximum_length int) as $$
select column_name, data_type, character_maximum_length
from INFORMATION_SCHEMA.COLUMNS where table_name = $1;
$$
language 'sql';
select * from describe_table('a_table_name');
You can do this in a single query by using array_agg() and a join on the information_schema.tables and information_schema.columns tables.
This would return something similar to your expected output:
select
t.table_name,
array_agg(c.column_name::text) as columns
from
information_schema.tables t
inner join information_schema.columns c on
t.table_name = c.table_name
where
t.table_schema = 'public'
and t.table_type= 'BASE TABLE'
and c.table_schema = 'public'
group by t.table_name;
Here I'm taking all the tables first, then I join it with the columns tables, and finally use array_agg() to aggregate them all to an array, grouped by the table name.
Hope it helps :) Feel free to ask if you have any doubts.
Since you're working in Python, clearest if you handle this in two steps I think. First, use this query to retrieve table/column name pairs:
select table_name, column_name
from information_schema.columns
where table_name in (
select tablename from pg_tables where schemaname = 'public');
Then, stick the results into a defaultdict:
from collections import defaultdict
my_cols = <your code to execute the query above and fetch all rows>
column_mapping = defaultdict(list)
for tablename, colname in my_cols:
column_mapping[tablename].append(colname)
This will give you:
>>> column_mapping
defaultdict(<type 'list'>, {'table_1': ['time', 'col'], 'table_2': ['time', 'col'], 'table_3': ['time', 'col]})
Which you can convert trivially with:
>>> column_mapping.items()
[('table_1', ['time', 'col']), ('table_2', ['time', 'col']), ('table_3', ['time', 'col])]