Sub topic 16
Visualizing Tables with the ERD Tool
This chapter assumes you know basic SQL and relational concepts such as tables, primary keys, and foreign keys. You also need a PostgreSQL server registered in pgAdmin 4 and permission to read the database objects.
An entity-relationship diagram (ERD) shows tables and the relationships between them. In pgAdmin, the ERD Tool creates a visual map from database metadata. A table appears as a box containing its columns. A line usually represents a foreign key relationship between two tables.
Open the ERD Tool
- Open pgAdmin and connect to the server.
- Expand Servers, the registered server, Databases, and your database.
- Select Tools, then ERD Tool. In some pgAdmin versions, you can also open the tool from a database or schema context menu.
- Add tables from the object browser or use the tool's table-selection controls.
- Arrange the table boxes so related tables are easy to follow.
The diagram is based on the objects that you add. It does not automatically show every table in the database unless you select or generate those tables.
Read relationships and constraints
A primary key uniquely identifies a row. A foreign key stores a value that refers to a key in another table. For example, orders.customer_id may reference customers.id.
The ERD helps you answer questions such as:
- Which table owns a piece of data?
- Which columns connect two tables?
- Is a relationship one-to-many or many-to-many?
- Which tables must be joined to answer a business question?
- Are expected relationships missing from the database definition?
The diagram reflects declared constraints, not relationships that merely seem likely from matching column names. If two tables both contain customer_id but no foreign key exists, pgAdmin may not draw a relationship between them.
| Diagram detail | What to check |
|---|---|
| Table box | Columns, data types, and key markers |
| Primary key | The column or columns that identify a row |
| Foreign key line | Referenced and referencing columns |
| Missing line | Whether a foreign key constraint is absent |
Verify the diagram with SQL
Use SQL to confirm an important relationship instead of relying only on the picture:
SELECT
constraint_name,
table_schema,
table_name,
column_name
FROM information_schema.key_column_usage
WHERE table_schema = 'public'
AND table_name = 'orders'
ORDER BY constraint_name, ordinal_position;The ERD is a discovery and communication tool. It does not replace checking constraints, nullability, indexes, or actual query results. Large databases can produce crowded diagrams, so begin with the tables relevant to one task.
Exercise
The ERD shows no line between orders.customer_id and customers.id, but both columns exist. What should you check first?
Show answer
Check whether a foreign key constraint from orders.customer_id to customers.id has been declared. If it is missing, verify the relationship with the database owner before adding a constraint.