I have a two tables in the database. First table has a foreign key to second table.
Can I get second table name by the foreign key from first table?
foreign keypostgresql
I have a two tables in the database. First table has a foreign key to second table.
Can I get second table name by the foreign key from first table?
Best Answer
Log in using
psql
, issueand look up the referenced table's name. If you set
\set ECHO_HIDDEN
on beforehand, you'll get a bunch of queries that produce the output, those you can reuse in your own discovery script.For example, on my test database these look like
The query you are interested in is:
With a minor change, you get the table name like