POSTGRESQL

Show Tables PostgreSQL Guide: Two Different Approaches

intro

Let’s look at the benefits of using show tables PostgreSQL approaches to retrieving a quick overview of tables in a database.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
PostgreSQL logo POSTGRESQL
THE POSTGRESQL DATABASE

PostgreSQL is a powerful and feature-rich relational database management system. When working with databases, it is essential to clearly understand the available tables. In this article, we will look at the SHOW TABLES PostgreSQL command, which allows you to retrieve a list of tables and gain insights into the database structure. Note that PostgreSQL does not support the SHOW TABLES statement directly like MySQL, but it does provide users with other alternatives.

Here, we will discuss the command's syntax and its implementation. Let’s get right into it!

Show Tables in PostgreSQL: Alternative Methods in PostgreSQL

This section will discuss two methods that PostgreSQL offers users to retrieve a list of tables in a database.

Method #1: Use the PSQL Shell

psql is a terminal-based tool where you can write and execute special command-line instructions. To connect to a particular database, open the terminal on your local computer and run the command below:

Copy
        
1 \c database_name

In this case, assume that the database name is postgres, so here’s how to connect to it:

Copy
        
1 \c postgres
Connecting to the Postgres database in the PSQL
Connecting to the Postgres database in the PSQL

This indicates that you are connected to the specified database. After connecting to a database, list the tables with this command below:

Copy
        
1 \dt

The command will then list all the available tables as shown here:

Using the PSQL to list the available tables
Using the PSQL to list the available tables

Using the PSQL to list the available tables

Great! We’ve now been able to list the available tables in our database using the PSQL shell. The next thing is to look at how to show the list of available tables using the pg_catalog schema method.

Method #2: Use the pg_catalog Schema

Another way to show tables in PostgreSQL is to use a SELECT statement to query data from a PostgreSQL system catalog table as below:

Copy
        
1 SELECT table_name 2 FROM information_schema.tables 3 WHERE table_schema = 'public' -- replace 'public' with the desired schema name 4 AND table_type = 'BASE TABLE';

The query retrieves the table names from the public schema. Replace public with the desired schema name to retrieve tables from a specific schema. The query filters out any non-base tables, such as views or foreign tables. In this use case, we’re going to employ DbVisualizer:

Using the pg_catalog schema to list all tables.
Using the pg_catalog schema to list all tables.

Using the pg_catalog schema to list all tables.

Here, it can be seen that the query has returned the list of tables available at database(postgres)>Schemas>public>Tables, which is the exact path for the tables looking on the left side of the DbVisualizer window pane.

Conclusion

The SHOW TABLES PostgreSQL command does not exist, sure. Yet, Postgres offers a few other ways to list tables in a database. One of the most useful of them involves using a tool that helps you manage databases and visually explore query results. This is where a full-featured database client like DbVisualizer comes in. In addition to being able to connect to several DBMSs, it offers advanced query optimization functionality, and full support for all database features, including listing tables. Download DbVisualizer for free now!

FAQ

Is there a SHOW TABLES command in PostgreSQL?

No, the SHOW TABLES command in PostgreSQL does not exist. It exists in MySQL, but not in PostgreSQL.

Can I see the data within a table using a show tables PostgreSQL approach?

No, the dt command in psql and pg_catalog table primarily focus on displaying metadata about tables. This includes information like table names, schema, columns, data types, and constraints. To view the actual data stored within a table, you need to use the SELECT statement.

Dbvis download link img
About the author
Leslie S. Gyamfi.
Leslie S. Gyamfi
Leslie Gyamfi is a mobile/web app developer with a passion for creating innovative solutions. He is dedicated to delivering high-quality products and technical articles. You can connect with him on LinkedIn
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

A Guide To the SQL DECLARE Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SERVER 7 min 2024-12-12
title

SQL Upsert: Inserting a Record If It Does Not Exist

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-12-10
title

How to Find Elements In an Array: PostgreSQL Guide

author Antonello Zanini tags POSTGRESQL 3 min 2024-12-05
title

How and Why to Add an Index in SQL

author Lukas Vileikis tags MySQL POSTGRESQL Search SQL SQL SERVER 8 min 2024-11-28
title

Postgres Create Array of Numbers: Complete Guide

author Lukas Vileikis tags POSTGRESQL SQL 7 min 2024-11-21
title

List of SQL Reserved Words in All Major Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 16 min 2024-11-20
title

SQL Variable: What It Is and How To Use It

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-11-18
title

Postgres DATEDIFF Equivalent Functions: Calculating Date and Time Differences

author Lukas Vileikis tags POSTGRESQL 6 min 2024-11-04
title

CREATE SCHEMA PostgreSQL Statement: What, Why & When to Use

author Lukas Vileikis tags POSTGRESQL 4 min 2024-10-30
title

SQL EXPLAIN: The Definitive Tool to Optimize Queries

author Antonello Zanini tags MySQL POSTGRESQL SQL 12 min 2024-10-29

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗