This guide explains how to list tables in your PostgreSQL database using simple commands and SQL queries. Perfect for beginners!
1. Using the \dt
Command in psql
(Easiest Way)
The psql
command-line tool has a quick command for listing tables.
Explanation:
The \dt
is a psql
meta-command that directly queries the system catalogs to display a formatted list of tables. It’s the quickest way to see tables if you’re already in the psql
shell.
Steps:
- Open your terminal or command prompt.
- Connect to your PostgreSQL database:
psql -U your_username -d your_database_name
- List tables in the current database:
\dt
Example Output:
This command shows tables in the current schema (usually public
).
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+----------
public | customers | table | postgres
public | orders | table | postgres
public | products | table | postgres
(3 rows)
2. Using SQL Queries (Standard Way)
You can also use standard SQL to list tables by querying PostgreSQL’s system catalogs .
Explanation:
These methods query specific system views (pg_tables
or information_schema.tables
) to retrieve table information. They offer more flexibility for filtering and are useful in scripts or when not using the psql
shell directly.
Option A: Query pg_tables
(PostgreSQL Specific)
Explanation:
pg_tables
is a PostgreSQL-specific view that lists tables. It’s often faster and provides direct access to PostgreSQL’s internal table information.
List tables in the public
schema:
SELECT tablename
FROM pg_tables
WHERE schemaname = 'public';
Example Output:
tablename
------------
customers
orders
products
(3 rows)
List tables in any schema (shows schema name too):
SELECT schemaname, tablename
FROM pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema'); -- Exclude system tables
Example Output:
schemaname | tablename
------------+------------
public | customers
public | orders
public | products
sales | invoices
(4 rows)
Option B: Query information_schema.tables
(Standard SQL)
Explanation:
information_schema.tables
is part of the SQL standard and provides a portable way to get table information across different database systems . Using table_type = 'BASE TABLE'
ensures you only see actual tables, not views.
List tables in the public
schema using the standard schema:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE'; -- Ensures we only get tables, not views
Example Output:
table_name
------------
customers
orders
products
(3 rows)
List tables in any user-defined schema:
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_schema NOT IN ('information_schema', 'pg_catalog') -- Exclude system schemas
AND table_type = 'BASE TABLE';
Example Output:
table_schema | table_name
--------------+------------
public | customers
public | orders
public | products
sales | invoices
(4 rows)
3. Listing Tables in Specific Schemas
If your database uses multiple schemas, you can filter the results.
Explanation:
By changing the schemaname
or table_schema
condition in your query, you can focus on tables within a particular schema.
Using pg_tables
:
SELECT tablename
FROM pg_tables
WHERE schemaname = 'your_custom_schema_name';
Using information_schema
:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'your_custom_schema_name'
AND table_type = 'BASE TABLE';
4. Filtering Tables by Name
If you have many tables, you can search for specific ones.
Explanation:
Using the LIKE
operator allows you to find tables whose names match a specific pattern.
Using psql
backslash command:
\dt *user* -- Lists tables with 'user' anywhere in the name
Using SQL (pg_tables
):
SELECT schemaname, tablename
FROM pg_tables
WHERE tablename LIKE '%user%'; -- Finds tables with 'user' in the name
Example Output:
schemaname | tablename
------------+------------
public | users
public | user_sessions
logs | user_activity_log
(3 rows)
Using SQL (information_schema
):
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_name LIKE 'prod_%' -- Finds tables starting with 'prod_'
AND table_type = 'BASE TABLE';
Example Output:
table_schema | table_name
--------------+------------
public | prod_catalog
inventory | prod_stock
(2 rows)
Quick Reference (Code Examples)
Need a quick reminder? Here are the essential commands with brief explanations:
- List tables in current
psql
database/schema:
(Uses\dt
psql
’s built-in command for a quick view ) - List tables in
public
schema (pg_tables):
(Queries PostgreSQL’s specific catalog view )SELECT tablename FROM pg_tables WHERE schemaname = 'public';
- List tables in
public
schema (information_schema):
(Uses the SQL-standard information schema for portability )SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE';
- List tables in any user schema (pg_tables):
(Shows tables from all non-system schemas )SELECT schemaname, tablename FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema');
- List tables in any user schema (information_schema):
(Shows tables from all non-system schemas using standard SQL )SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema NOT IN ('information_schema', 'pg_catalog') AND table_type = 'BASE TABLE';
- List tables with names containing ‘log’ (pg_tables):
(Filters tables by name pattern)SELECT tablename FROM pg_tables WHERE tablename LIKE '%log%';
- Switch database in
psql
:
(Changes your active database connection within\c your_database_name
psql
)
Common Issues & Solutions
- No tables listed?
- Check you are connected to the correct database:
\c correct_database_name
- Check if you are looking in the right schema.
- Check you are connected to the correct database:
- Permission errors?
- You might not have access to view certain schemas or tables. Contact your DBA.
- Too many tables?
- Use the
LIKE
operator in your SQL query to filter names (see Filtering examples above). - Or, in
psql
, use\dt pattern
(e.g.,\dt user*
).
- Use the
Conclusion
Listing tables in PostgreSQL is straightforward using either the \dt
command in psql
or standard SQL queries against system catalogs . Use the method that best fits your workflow. The code examples above provide ready-to-use snippets for common scenarios.