\l
Example: Run this after connecting with `psql` to see available databases on the current server.
Gotcha: This is a `psql` meta-command, not SQL. It works inside the `psql` client only.
This page favors everyday Postgres tasks developers reach for repeatedly in local work, migrations, and troubleshooting.
\l
Example: Run this after connecting with `psql` to see available databases on the current server.
Gotcha: This is a `psql` meta-command, not SQL. It works inside the `psql` client only.
\dt
Example: A quick inventory command when you need to see what tables are available in the connected database.
Gotcha: Like other backslash commands, this is for `psql`, not for application SQL execution.
\d users
Example: Use this to inspect columns, indexes, and related table metadata from inside `psql`.
Gotcha: Add `+` for more detail: `\d+ users`.
SELECT pg_size_pretty(pg_total_relation_size('users'));
Example: Useful for spotting unexpectedly large tables in local or staging environments.
Gotcha: Total relation size includes indexes and toast storage, not just raw table rows.
SELECT * FROM users LIMIT 10;
Example: Useful for quickly checking table contents without dumping the whole dataset.
Gotcha: Without `ORDER BY`, the first rows returned are not guaranteed to be meaningful or stable.
Postgres rewards precision. Small choices in types, constraints, joins, and indexing often matter, and the command-line experience adds a second layer: SQL plus psql meta-commands. This reference page is designed for the daily work developers actually do in PostgreSQL: inspect databases, list relations, describe schema, query rows, and define constraints without reopening a full manual every time.
If you searched for a Postgres reference tutorial, an example of Postgres references foreign key, or a reminder about REFERENCES ON DELETE CASCADE, you are in the right place. The aim here is practical PostgreSQL, not abstract SQL theory.
psql mode. Both matter in PostgreSQL workflows.psql patterns| Task | Command or statement | Why it helps |
|---|---|---|
| List databases | \l |
Fast overview inside psql |
| Connect to a database | \c app_db |
Switch context without reopening the client |
| List tables | \dt |
Quick relation inventory |
| Describe a table | \d orders |
Columns, types, defaults, indexes |
| Run a targeted query | SELECT ... LIMIT 25; |
Inspect real data without overload |
| Check server time | SELECT NOW(); |
Useful sanity check for sessions and timestamps |
| Analyze a query plan | EXPLAIN ANALYZE ... |
Performance visibility |
| Define a foreign key | REFERENCES customers(id) |
Keeps row relationships valid |
psql when the schema is unclear```sql
\l
\c app_db
\dt
\d orders
SELECT id, customerid, status, createdat
FROM orders
ORDER BY created_at DESC
LIMIT 20;
```
This sequence is often faster than guessing table names or column types. PostgreSQL developers sometimes underuse psql meta-commands, but they are one of the reasons Postgres is so productive from the command line. A practical reference should keep both SQL and psql in view.
REFERENCES in PostgreSQLA foreign key in Postgres expresses a relationship that the database enforces. The REFERENCES clause points to the target table and column, usually a primary key. That gives you reliable integrity at the storage layer instead of hoping the application always behaves correctly.
```sql
CREATE TABLE orders (
id BIGSERIAL PRIMARY KEY,
customer_id BIGINT NOT NULL REFERENCES customers(id) ON DELETE RESTRICT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
```
When developers compare Postgres REFERENCES vs foreign key, the practical answer is that REFERENCES is part of the foreign-key definition. You can write it inline on the column or as a named table constraint. The underlying concept is the same: enforce a relationship.
ON DELETE CASCADEON DELETE CASCADE can be helpful when child rows should disappear automatically with the parent. For example, deleting an order might reasonably delete its order items. But cascade rules should reflect real ownership, not convenience. If the child row has value or history on its own, automatic deletion may be the wrong rule.
This is why a practical Postgres reference should not only show the syntax, but also the design question behind it. The SQL is easy to copy. The data lifecycle decision is where mistakes usually happen.
Postgres is flexible, but a few habits keep your queries easier to reason about:
\d before joiningEXPLAIN ANALYZE when performance questions appearpsql convenience commandsThat last point matters more than it seems. A command like \dt is not SQL. It is a psql feature. Keeping that mental boundary clear makes it easier to move between clients and scripts.
| Mistake | Why it causes trouble |
|---|---|
Forgetting whether a command is SQL or psql |
The statement fails in another client or script |
Using SELECT * too long during exploration |
Noise grows and intent becomes unclear |
| Adding cascade rules casually | Deletes can become broader than intended |
| Skipping table inspection before writing joins | Column or relation assumptions go wrong |
| Treating PostgreSQL exactly like another SQL dialect | Small syntax and behavior differences matter |
Use the full PostgreSQL manual when you need deep semantics or version-specific behavior. Use a page like this when you need the common command or design pattern first.
REFERENCES do in Postgres?It declares a foreign-key relationship so a value in one table must match a valid row in another table.
REFERENCES different from a foreign key?REFERENCES is part of how you express a foreign key. Inline column syntax and explicit table constraints both create the same kind of relationship.
ON DELETE CASCADE?Use it when child rows are owned by the parent and should not outlive it. Avoid it when child records need separate retention or audit value.
Use the MySQL reference for dialect comparison and shared SQL tasks, the VS Code shortcut reference for editor-side navigation while reviewing schema code, the Vim reference for database file editing without leaving the keyboard, or the Bash reference when psql commands become shell automation.
Simplicity is the soul of efficiency.
…
…