Search This Blog

Tuesday, October 6, 2009

Generate a Data Dictionary from your Postgres Database tables?

Hello,

Recently we had a requirement on our Application to generate a Data Dictionary for the Postgres Database.

This is one way I found with reference to some forums:

select * from information_schema.columns where table_schema = 'public' order by table_name;

Fire the above Query and execute the results to a .csv file.
You will find a briefing of all the Database tables with a lot of information. Tweak the required info accordingly.

Here is one tweaked query that we used,

SELECT
isc.table_name,
--isc.ordinal_position::integer AS ordinal_position,
isc.column_name::character varying AS column_name,
isc.column_default::character varying AS column_default,
isc.data_type::character varying AS data_type,
isc.character_maximum_length::integer AS str_length,
isc.is_nullable,
CASE
WHEN isc.udt_name::text = 'int4'::text OR isc.udt_name::text = 'bool'::text THEN isc.data_type::character varying
ELSE isc.udt_name::character varying
END AS udt_name
FROM
information_schema.columns isc
WHERE
isc.table_schema::text = 'public'::text
ORDER BY
isc.table_name, isc.ordinal_position;

Do let me know if you find any more elegant ways to do this.

Thank You.

No comments: