Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Describe all tables to text file

Tags:

postgresql

How can one describe every table in a database and export all the results to a text file? E.g.

\o describe.txt
\d+ MY_TABLE
\o

but for every table, appending the output to the text file each time.

like image 257
nacnudus Avatar asked Nov 15 '25 20:11

nacnudus


2 Answers

I think you might want:

\dt+ *.*

i.e "all tables in all schemas". Use \d instead of \dt if you want to include views too.

I can't help wondering ... why? If you're doing this for documentation check out SchemaSpy as a much nicer alternative.

like image 129
Craig Ringer Avatar answered Nov 18 '25 19:11

Craig Ringer


To get output by SQL query, please see below two ways:

(1) All column names and datatypes comma separated:

SELECT     
table_name, 
string_agg(column_name,',') as "Columns", 
string_agg(data_type ,',') as "DataTypes"
 FROM 
 information_schema.columns
 where "table_schema"='public'
  group by table_name

(2) All column names separately with datatypes

SELECT 
table_name, 
column_name as "Columns", 
data_type as "DataTypes"
 FROM 
 information_schema.columns
 where "table_schema"='public'
  order by table_name,column_name
like image 41
Rohil Patel Avatar answered Nov 18 '25 20:11

Rohil Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!