Delinea Blog > How to show and list tables in PostgreSQL

How to show and list tables in PostgreSQL

Published May 2025
Read time 5 minutes
What you will learn
Listing tables in PostgreSQL is a basic yet essential task—whether you're debugging, automating CI/CD, or exploring a new database. But with sensitive data at stake, how you access matters. This guide covers common methods and how to secure them with centralized access, audit logs, and least-privilege controls.

There are four methods for listing the tables in a PostgreSQL database: a SQL query, the psql \dt command, the pgAdmin tree, or a script in Python or Node.js. Each takes seconds. Each also runs on a credential that can read far more than table names.

Whether you’re debugging queries, automating CI/CD workflows, or just getting familiar with a new database, this guide works through all four. 

But access comes with responsibility, especially when your database holds sensitive data. Knowing how to list tables matters less than knowing how to list them without leaving an unaudited path to the rows behind them.

The first steps to listing tables

Before you use any of these methods, you need: 

  • an installed PostgreSQL server.
  • user credentials, usually a username and password, depending on whether the server needs authentication.
  • access rights, such as CONNECT and USAGE on the relevant schema plus SELECT on the system catalogs to read table metadata.
  • a client such as psql, pgAdmin, or psycopg2.

The three table types in PostgreSQL 

PostgreSQL tables can be broadly categorized into three types: user-defined, system and temporary tables. 

The end user creates user-defined tables to hold application-specific data. The database vendor ships the system tables or catalogs, which hold metadata about the database's configuration, objects and structure and live in pg_catalog. 

 Temporary tables exist for a single session or transaction and hold working data such as the results of a complex query. PostgreSQL drops them when the session or transaction ends. 

Method 1: SQL queries

Two SQL queries retunr the table list. Pick the one that matches the metadata you need.  

Using information_schema.tables

The information_schema lets you list the tables in a PostgreSQL database and view metadata for database objects. 

Its basic syntax is: 

SELECT table_schema, table_name 
 FROM information_schema.tables  
 WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema');

While information_schema also works on other relational databases such as SQL Server and MySQL, but it leaves out some PostgreSQL-specific metadata.

Using pg_catalog.pg_tables

This queryretuns PostgreSQL-specific context that information_schema omits, including table ownership and tablespace.

Its syntax is:

SELECT schemaname, tablename FROM pg_catalog.pg_tables 
 WHERE schemaname NOT IN ('pg_catalog', 'information_schema');

Method 2: Using psql command-Line

 The psql command-line tool connects you to a PostgreSQL database from a terminal. The \dt command lists the tables. 

  1.  Open a terminal and connect to the database you want: psql -U postgres -d dvdrental 
  2.  Run \dt to list the tables in the current search path. 

In place of \dt, you can also use the following commands, depending on the information you want to display:

  •  \dt+ adds extended information such as table size and description. 
  • \dt public.*: lists the tables in the public schema only
  • /dt*.* lists the tables in every schema including the system catalogs.

Method 3: the pgAdmin interface

 pgAdmin is a graphical interface for managing PostgreSQL databases. Its tree view makes it the fastest route for anyone who would rather not work in a terminal: 

  1. Launch pgAdmin and connect to your PostgreSQL server.
  2. Expand the Servers dropdown menu on the left, then Databases, and select the one you want. 
  3. Expand the Schemas group option > public > Tables in the tree.  

Method 4: programmatic access

 Python and Node.js both connect to a PostgreSQL database and run the same catalog query. Here's how: 

Python with psycopg2

import psycopg2  
 conn = psycopg2.connect(...) 
 cur = conn.cursor() 
 cur.execute("""     
 SELECT table_schema, table_name 
 FROM information_schema.tables  
 WHERE table_type = 'BASE TABLE' 
 AND table_schema NOT IN ('pg_catalog', 'information_schema'); """)
 print(cur.fetchall()) 
 cur.close()  
 conn.close()

Node.js with pg

const { Client } = require('pg'); const client = new Client({ /* connection config */ }); client.connec t(); client.query ( `SELECT table_schema, table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema')`, (err, res) => { if (err) throw err; res.rows.forEac h(row => console.log (row)); client.end (); } );

Four controls that keep table listing accountable

 Every method above works. Every one also runs as a database user with rights to read the catalog, which turns a routine listing into an exposure when the account is over-permissioned. Four controls reduce that risk: 

  • Keep passwords out of source code.  Embedded credentials hand a cyber criminal the database. Use a secrets manager such as HashiCorp Vault or AWS Secrets Manager, or a local credentials file such as .pgpass. PostgreSQL documents the libpq  environmental variables and advises against PGPASSWORD on a multi-user host, where other users can read a process environment.
  • Grant the least privilege the task needs.  A listing job needs CONNECT, USAGE and SELECT on the catalog. It does not need the rows. Least privilege is easier to hold when the account is scoped at creation rather than trimmed after an audit. 
  • Log every session. Record who connected, from where and what they ran, then send it somewhere a person reads. 
  • Route access through one controlled path. One access layer in front of the database gives you one place to apply policy and one audit trail to search. 

Troubleshooting common errors

Four errors come up most often. Each has a one-line fix.

Issue Cause Solution
Permission denied Insufficient access Check the user's access rights
Schema not found Incorrect name
  • Verify spelling and visibility
  • Check the current search path: SHOW search_path
psql: not found Missing client Install PostgreSQL tools
pgAdmin not syncing Cached UI Refresh the browser or reconnect the server

Automating table discovery in CI/CD

 A pipeline can list tables on every run to check a schema or build an inventory.  In Bash: 

psql -U your_user -d your_db -c "\dt *.*" > tables_list.txt

That command lists the tables in every schema and writes them to tables_list.txt, ready for a database inventory, an audit or a schema diff. 

Where Delinea fits

Listing tables is read-only. The access behind it is not. The same connection that returns table names can also return rows, and in most environments, it runs under a credential that is issued once and reviewed rarely. Delinea puts the credential and the session under policy instead of under a developer's control: 

  • issue database access through your identity provider rather than a shared password
  • grant it for the task and expire it when the task ends
  • record the session so the query stays attributable months later

That is the same zero trust principle you already apply to a login, applied to the database connection behind it. You keep every method in this guide. What changes is who holds the credential and whether anyone can answer, a quarter later, who listed those tables and why.

See how database access runs under policy: book a demo.

 

Related Topics