Postgresql

PostgreSQL query to list all table names

25 September 2026 · 4 min read

PostgreSQL query to list all table names

Navigating the vast landscape of a PostgreSQL database can sometimes feel like exploring uncharted territory. One of the most fundamental tasks any developer or database administrator needs to perform is identifying the tables within a database. Knowing how to efficiently list all table names in PostgreSQL is crucial for schema exploration, data management, and various other database operations. This article will provide a comprehensive guide to achieving this, exploring different methods and best practices for querying table names in PostgreSQL.

Using the information_schema

The information_schema is a standardized set of views that provide information about database objects. It’s a powerful tool for introspection and is highly consistent across different database systems. In PostgreSQL, you can use the tables view within information_schema to retrieve table names.

The following query demonstrates how to retrieve all table names in the current database:

SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';

This query filters the results to only show tables within the public schema, which is the default schema in PostgreSQL. If your tables reside in different schemas, you can modify the table_schema clause accordingly. For instance, to list tables in the ‘my_schema’ schema:

SELECT table_name FROM information_schema.tables WHERE table_schema = 'my_schema';

Listing Tables with pg_catalog

For more PostgreSQL-specific operations, the pg_catalog contains system tables and views providing a lower-level view into the database structure. This approach can be slightly more complex but offers greater flexibility and access to more detailed information about tables.

The following query utilizes pg_catalog.pg_tables to list table names:

SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public';

Similar to the information_schema approach, you can adjust the schemaname clause to filter tables from different schemas. pg_catalog generally offers more detailed information about tables compared to information_schema, making it useful for advanced use cases.

Filtering Table Names with Wildcards

Both the information_schema and pg_catalog approaches allow using wildcards to filter table names based on patterns. This is useful when you need to find tables matching specific criteria.

For instance, to list all tables starting with “customer_” in the public schema:

SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name LIKE 'customer_%';

This allows for more granular control over the listed tables, improving efficiency when dealing with large databases.

Practical Applications and Examples

Listing table names is a foundational step in many database operations. Consider a scenario where you need to generate reports based on data from several related tables. Listing the tables first allows you to quickly identify the relevant tables and build your queries accordingly.

Another example is database migration. Before migrating data to a new system, understanding the existing table structure is crucial. Listing all tables provides a clear overview of the data that needs to be transferred.

Imagine you’re working with a large database with hundreds of tables. Using these queries with wildcards or specific schema filtering helps narrow down the results, focusing only on the relevant tables, saving valuable time and resources.

  • Use information_schema for standard SQL-compliant access to table names.
  • Utilize pg_catalog for PostgreSQL-specific operations and detailed table information.
  1. Connect to your PostgreSQL database.
  2. Execute the appropriate query based on your requirements.
  3. Process the returned table names within your application or script.

For a more in-depth understanding of PostgreSQL administration, refer to the official PostgreSQL documentation.

[Infographic Placeholder: Visual representation of querying table names in PostgreSQL]

Efficiently listing table names is fundamental for navigating and managing your PostgreSQL database. Whether you’re exploring the schema, building complex queries, or preparing for data migration, understanding these techniques empowers you with better control over your data. By leveraging the information_schema or pg_catalog, combined with wildcard filtering and schema selection, you can effectively target specific tables and optimize your database workflows. Explore these techniques and integrate them into your PostgreSQL toolkit to enhance your database management capabilities. Learn more about database management best practices on this website and dive deeper into SQL query optimization on this page. Also, check out our related article for more practical tips on PostgreSQL.

  • Remember to choose the appropriate schema for your query.
  • Use wildcards for efficient filtering of table names.

FAQ

Q: How can I list tables from a specific schema other than ‘public’?

A: Modify the table_schema (in information_schema) or schemaname (in pg_catalog) clause in your query to match the desired schema name. For example: SELECT table_name FROM information_schema.tables WHERE table_schema = 'my_schema';

Question & Answer :
Is there any query available to list all tables in my Postgres DB.

I tried out one query like:

SELECT table_name FROM information_schema.tables WHERE table_schema='public' 

But this query returns views also.

How can i get only table names only, not views?

What bout this query (based on the description from manual)?

SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE';