> For the complete documentation index, see [llms.txt](https://docs.whaly.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.whaly.io/connectors/source-catalog/database/postgres/tip-extracting-the-relationships.md).

# Tip: Extracting the relationships

In order to create the proper relationships between your database tables in Whaly workbench, you can use the above SQL queries to extract them.

```
SELECT 
    tc.table_schema, 
    tc.constraint_name, 
    tc.constraint_type, 
    tc.table_name, 
    kcu.column_name, 
    ccu.table_schema AS foreign_table_schema, 
    ccu.table_name AS foreign_table_name, 
    ccu.column_name AS foreign_column_name
FROM information_schema.table_constraints AS tc 
JOIN information_schema.key_column_usage AS kcu 
ON 
    tc.constraint_name = kcu.constraint_name 
    AND 
    tc.table_schema = kcu.table_schema 
JOIN information_schema.constraint_column_usage AS ccu 
ON 
    ccu.constraint_name = tc.constraint_name 
    AND 
    ccu.table_schema = tc.table_schema;
```

&#x20;If you want to export those into a CSV file, you can run directly in Postgres:

```
\copy (SELECT tc.table_schema, tc.constraint_name, tc.constraint_type, tc.table_name, kcu.column_name, ccu.table_schema AS foreign_table_schema, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema;) to 'whaly.csv' csv header
```

This will write on your Postgres server a whaly.csv file with all the relationships between the tables.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.whaly.io/connectors/source-catalog/database/postgres/tip-extracting-the-relationships.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
