AI Generated. Credit: Google Gemini
When working with PostgreSQL databases, one of the most common tasks developers perform is checking which tables exist in a database. Whether you’re debugging, exploring a new schema, or validating migrations, knowing how to list tables in PostgreSQL quickly can save a lot of time.
In this blog, you’ll learn multiple ways to list tables using psql commands and SQL queries, along with common mistakes and best practices.
There are two primary approaches to listing tables in PostgreSQL:
Both methods are useful depending on your workflow.
The psql command-line interface provides simple shortcuts to inspect database objects.
\dtThis command lists all tables in the current database and schema (usually public).
This is the fastest way to postgresql list of tables when you’re working locally.
\dt schema_name.*Example:
\dt public.*If your tables are not in the default schema, this command helps avoid confusion when tables seem to be missing.
\dt+This displays additional details like table size and descriptions, which is helpful when analyzing large databases.
If you’re working in applications, dashboards, or automation scripts, SQL queries are more flexible than psql commands.
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public';This method is portable and works across different database tools.
PostgreSQL exposes table metadata through the official PostgreSQL documentation on information_schema, which is useful when working across different schemas and environments.
SELECT tablename
FROM pg_tables
WHERE schemaname = 'public';This approach is PostgreSQL-specific but faster for system-level queries.
Before listing tables, make sure you are connected to the correct database.
\c database_nameAfter connecting, run:
\dtMany issues related to postgresql show tables happen because developers are connected to the wrong database.
| Method | Best Use Case |
|---|---|
| psql commands | Quick manual checks |
| SQL queries | Automation and applications |
If you’re building scripts or tools, SQL queries are more reliable than interactive commands.
Here are a few reasons why tables may not appear:
If you also manage users, reviewing postgresql list users and permissions can help resolve access issues.
public schema.These best practices align well with software development best practices and help prevent environment-related mistakes.
Knowing how to list tables in PostgreSQL is a foundational skill for anyone working with relational databases. Use psql commands for quick inspections and SQL queries when working with scripts or applications. By understanding schemas and permissions, you will avoid common pitfalls and work more efficiently.
Use the command:
\dtThis lists tables in the current schema.
You may be connected to the wrong database, using the wrong schema, or lacking permissions.
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE';