Sub topic 16
Visualizing tables with the ERD tool
This chapter assumes you can connect to a server in pgAdmin 4, browse the object tree, and read basic table definitions with \d+ in psql. You should also know what primary keys and foreign keys are. Here you will use pgAdmin's ERD tool to draw the tables of a database and the relationships between them.
What the ERD tool is for
An entity-relationship diagram (ERD) shows tables as boxes and foreign keys as lines between them. The pgAdmin ERD tool is a graphical editor built into pgAdmin 4. It does two jobs:
- It reads an existing schema and draws it, so you can learn an unfamiliar database quickly.
- It lets you design new tables on a canvas and generate SQL for them.
When you explore a database, the first job matters most. With psql you find relationships one table at a time, by reading the Foreign-key constraints section of \d+ orders. The ERD tool shows many of those relationships at once.
The tool needs a recent pgAdmin 4 (the feature arrived in version 4.26) and a connection to a PostgreSQL server. It is not available for other database engines in pgAdmin.
Opening the tool
You can open the ERD tool from the object browser in three ways:
- Right-click a database and choose ERD For Database. This loads every table in the database.
- Right-click a schema, or select it and use the Tools menu, to open the diagram for that schema.
- Right-click a single table and choose ERD For Table. This loads that table and the tables it is linked to by foreign keys.
Choosing the whole database is tempting, but on a database with hundreds of tables the canvas becomes unreadable. Start from one table and add its neighbours as you need them.
The exact menu names differ slightly between pgAdmin versions. If you do not see one of these entries, look in the Tools menu while the database or table is selected.
Reading the diagram
Each table appears as a box. The box lists the table name at the top and the columns below it. Each column row shows the column name and its data type. Primary key columns are marked with a key icon.
Lines between boxes represent foreign keys. A line starts at the referencing column (the "many" side, for example orders.customer_id) and ends at the referenced column (the "one" side, for example customers.id). The ends of the line carry symbols that show cardinality. These are the usual crow's-foot symbols for "one" and "many".
The diagram below shows the kind of structure you would see for a small shop schema, using the same orders table as the rest of this tutorial.
Read customers ||--o{ orders as: each order belongs to exactly one customer, and a customer may have zero or more orders. The ERD tool in pgAdmin draws its own symbols, but the meaning is the same.
What the diagram does not show
The ERD tool draws only relationships that exist as foreign key constraints. Many real databases have columns such as customer_id that are never declared as foreign keys. The application enforces the link, or nobody does. In that case the tables appear unconnected even though they are related.
This is a useful finding in itself. If a table has a column named like another table's key but no line, check with psql:
SELECT conname,
conrelid::regclass AS referencing_table,
confrelid::regclass AS referenced_table,
pg_get_constraintdef(oid) AS definition
FROM pg_constraint
WHERE contype = 'f'
AND connamespace = 'public'::regnamespace
ORDER BY conrelid::regclass::text;This lists every foreign key constraint in the public schema from the pg_constraint catalog. If the query returns no row for a pair of tables, the database does not enforce a link between them, and the ERD tool will not draw one.
Working on the canvas
The toolbar above the canvas gives you the main controls. Names vary a little by version, so hover over an icon to see its label.
| Action | What it does |
|---|---|
| Zoom in, zoom out, zoom to fit | Changes the view. Zoom to fit is the fastest way to see the whole diagram. |
| Auto align | Rearranges boxes to reduce crossing lines. |
| Add table | Adds a new, empty table box for design work. |
| Add or edit a relationship | Draws a foreign key between two columns. |
| Generate SQL | Produces the SQL for the changes you made on the canvas. |
| Save and open | Stores the diagram in a file with an `.pgerd` extension. |
| Download image | Exports the canvas as an image, useful for documentation. |
You can drag boxes to group related tables. Positions are stored only in the diagram, so moving a box never changes the database.
Adding related tables
When you start from one table, the diagram shows only that table and its direct neighbours. To follow a relationship further, add the other tables from the object browser by using the diagram's table-adding control. Repeat this until you can see the area of the schema you care about. This keeps the diagram small and readable.
Viewing versus changing
The ERD tool starts in a mode that mirrors the live database. Be careful here. If you add a table, edit a column, or draw a relationship, you are editing the diagram. Nothing changes on the server until you generate SQL and run it.
The usual workflow for design work is:
- Make the change on the canvas.
- Choose Generate SQL. pgAdmin opens the Query Tool with
CREATE TABLEandALTER TABLEstatements. - Read the SQL. Check it against your intent.
- Run it, preferably in a transaction on a development database first.
BEGIN;
CREATE TABLE public.shipments (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
order_id integer NOT NULL,
shipped_at timestamp
);
ALTER TABLE public.shipments
ADD CONSTRAINT shipments_order_id_fkey
FOREIGN KEY (order_id) REFERENCES public.orders (id);
ROLLBACK;This is the kind of script the tool generates, with the transaction wrapper added by hand. Using ROLLBACK lets you confirm the statements run without error and leave nothing behind. Change it to COMMIT when you are satisfied.
Limitations and common problems
- Large schemas. A diagram with many tables is slow and hard to read. Work from a single table or a single schema.
- Missing lines. Lines come only from declared foreign keys. See the catalog query above.
- Composite keys. A foreign key over several columns is drawn as a single line. Check the constraint definition to see which columns it uses.
- Cross-schema links. Tables in other schemas appear only if you add them to the diagram.
- Not a live view. The diagram is a snapshot taken when you opened it. If someone changes the database afterwards, reopen the tool to see the change.
- Permissions. You need to be able to read the catalog entries of the tables. Objects you cannot see in the object browser will not appear.
ERD tool compared with psql
| Task | psql | pgAdmin ERD tool |
|---|---|---|
| See the foreign keys of one table | `\d+ orders` | Open the table in the ERD tool |
| See how ten tables connect | Several `\d+` commands or a catalog query | One diagram |
| Find undeclared relationships | Catalog queries and naming checks | Visible only as missing lines |
| Share a picture with a team | Not practical | Download an image |
| Run in a script or over SSH | Yes | No |
Use the diagram to get a map of the schema, then return to psql or the Query Tool for exact details such as constraint definitions, row counts and sizes.
Exercise
You open the ERD tool for a database and see the tables customers and orders with no line between them, although orders has a column called customer_id. What does this tell you, and how can you confirm it?
Show answer
The ERD tool draws only declared foreign key constraints, so the database has no foreign key from orders.customer_id to customers. The application may enforce the link, or it may not be enforced at all. Confirm it by querying pg_constraint where contype = 'f', or by running \d+ orders in psql and checking that the Foreign-key constraints section is missing or does not list customers.
Exercise
You use the ERD tool to add a new table and a foreign key to an existing database. You close the diagram without running anything. Does the new table exist on the server? Why or why not?
Show answer
No. Changes made on the canvas change only the diagram. The server changes only when you generate the SQL and run it in the Query Tool.