Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL command to show entity relations

At the start I want to point that I am not and I never was database administrator. I have got experience and knowledge about PL/SQL and SQL but so far each database i get touch with was max 50 tables and looking for relation between entities wasn't that hard even if I made it manually.

Problem comes now, when I have got database which have over 300 tables finding relation between entities is not so easy.
Let imagine table:

table person (
id serial PRIMARY KEY NOT NULL,
name varchar(45)
)

and automaticly i have sequence:

person_id_seq


Can you please tell me if there is any command which return me tables which are using person_id_seq?

like image 302
Grzzzzzzzzzzzzz Avatar asked Aug 31 '25 04:08

Grzzzzzzzzzzzzz


2 Answers

See pg_depend.

select *, pg_describe_object(classid, objid, objsubid)
from pg_depend 
where refobjid = 'person_id_seq'::regclass
like image 163
klin Avatar answered Sep 03 '25 10:09

klin


klin's answer - use pg_depend is entirely sensible, and a sane way to do this.

I strongly recommend that you try a tool like SchemaSpy to produce database maps and visualisations. It will help a lot, and give you a chance to get an overview of the relationships, not just explore them piece by piece.

If you need to make your query portable then you can use the information_schema instead of PostgreSQL-specific catalog tables. This query produces a list of foreign key relationships portably - the query was written for MS SQL Server, but is fine on PostgreSQL.

like image 33
Craig Ringer Avatar answered Sep 03 '25 09:09

Craig Ringer