Link to home
Start Free TrialLog in
Avatar of Richard2048
Richard2048

asked on

Postgres Foreign Keys

I wish to write a query that will return a list of foreign keys and the table to which they pertain.
For example:
- Given a table of 'customers' and a table of 'accounts', in which each customer can have 1 or more accounts
- The FK in accounts is a customer_id

How would I write a query to list the foreign key(s) in the accounts table along with the table to which said foreign key relates.
For exampe:
I want to query the 'accounts' table, so as to retrieve the customer_id as the FK and the customers table as the table linked to this FK.

Any ideas?
Avatar of earth man2
earth man2
Flag of United Kingdom of Great Britain and Northern Ireland image

psql --quiet --echo-hidden dbname
\d accounts
ASKER CERTIFIED SOLUTION
Avatar of cminear
cminear

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Richard2048
Richard2048

ASKER

Perfect,
For the most part, all I have are single column references.  The second query you posted certainly brought a smile to my face!

Thankyou!