EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL List Tables
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

PostgreSQL List Tables

By Payal UdhaniPayal Udhani

PostgreSQL List Tables

Introduction to PostgreSQL List Tables

Whenever you work on the database, it is very common to retrieve the tables and their structure present in your database as it gives you a general idea of what are all the entities that are being stored in your database. Whenever you create a new database in PostgreSQL, it contains many default tables, which are called the system tables. These tables are used internally by the database to perform operations and store system variables, operators and functions. Other than this, the system tables also maintain the data about SQL and strategic parameters that decide how any operation will be executed and the main constraints to be taken care of while executing any command. Also, information about which tables are locked currently for updation if any query of retrieval is fired on that table is maintained in system tables.

Other than the system tables, we can create many user-defined tables for our usage. This article will learn various methods that can be used to retrieve the tables present in our database and list them out.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Listing Out Tables in PostgreSQL

In case if you are familiar with MySQL before then, you must be thinking to use the query of SHOW TABLES; to retrieve the list of all the tables in the current database. However, in PostgreSQL, there is no such query. In PostgreSQL, we can retrieve the list of tables by either using \dt command when you are using psql or retrieve the list of tables using the SELECt query from the pg_tables table of pg_catalog schema. We will see how we can use both of these methods one by one. For this, you will first need to open the terminal and login to PostgreSQL with your username with command

sudo su – postgres

and then enter your password. And press enter. Then open the psql shell by typing the command

psql

and then click on enter. The output will be somewhat like shown below.

Output-1.1

The next step is to check all the databases present in your current database server. For this, you can either type

\l or \list

the command to list out all databases and schemas. It will list out all the schemas and databases, and the output will be like this.

Output-1.2

The three databases, Postgres, template0 and template1, are default databases, while the demo database was created by me before. For creating a new database, you can use the command createdb and the name of the database. For example, let us create a database named educational_platform. For this, we will fire the following command.

createdb educational_platform

Let us check whether our database is created by entering \l command

\l

Output-1.3

Now to switch between database and use the database of your choice, you can use the command \c or \connect and the name of the database to which you want to connect. Now, fire command

\c educational_platform

The output will be as follows:

Output-1.4

Listing Out Tables using psql metacommands

Now we will learn how to list the tables using psql metacommands. Metacommands are short commands that are compact and easy to use for database administrators as the psql evaluates this and converts it to SQL queries if required and stored in system tables. We can use either

\dt or \dtables

metacommand to list out the user-created and user-defined tables. The output will be as follows –

PostgreSQL List Tables-2.1

As there are no tables present in our newly created database its saying that Did not find any relations. Let’s create a new table and check whether it is getting retrieved or not.

CREATE TABLE educba
(id INTEGER PRIMARY KEY,
technologies VARCHAR,
workforce INTEGER,
address VARCHAR);

PostgreSQL List Tables-2.2

Now, let us check whether the table is added by entering the command

\dt or \dtables

PostgreSQL List Tables-2.3

So we can see now that our table is added successfully.

Listing Out Tables using SELECT query

Now we will learn how we can use the simple SQL query of SELECT to retrieve information about all the tables present in our current database. The information of all the tables is stored in the table named pg_tables of pg_catalog schema. Now, as we are going to fire the query on the table present in some other database or schema than the one we are currently connected to, we will need to specify the name of the schema explicitly, and then dot(.) separated table name to list out the tables. So our query should be like this.

select * from pg_catalog.pg_tables;

The output of the above query will be somewhat like shown below.

PostgreSQL List Tables-3.1

PostgreSQL List Tables-3.2

It will be a long list displaying all the tables that are system tables and user-defined tables. What if we only want to retrieve user-defined tables and not the system tables. For this, we will have to apply the constraint in the WHERE clause specifying the tables should not belong to pg_catalog or information_schema schemas as these two schemas only have internal system tables present in them. So our query should be arranged in the way, as shown below.

SELECT * FROM pg_catalog.pg_tables
WHERE schemaname != 'information_schema' AND
schemaname != 'pg_catalog';

The output of the above query will be as shown below.

PostgreSQL List Tables-3.3

As it can be seen that only the user-defined table “educba” created by us is retrieved, which is the expected output. The pg_tables table displays table name and many properties related to the table, which includes schemaname, tableowner, tablespace, hasindexes, hasrules, hastriggers and rowsecurity. Most of the fields’ answers have the values t and f, which are boolean type parameters specifying whether the table has that property or not. For example, in our table, the column hastriggers have value f, which means that the current table educba does not have any triggers defined on it. Similar is the case for other columns. Besides this, the columns schemaname and tableowner contain the table belongs to which type of schema and who owns this table.

Conclusion

In PostgreSQL, we can list the tables in two ways: using the psql meta-commands of simple SELECT clause query on the table pg_tables of pg_catalog schema. Both these queries result in the same output. The difference is just that the metacommand returns only user-created tables while the SELECT query results in the system and user-defined tables. We need to apply constraints to the SELECT query to list only user-created tables.

Recommended Articles

This is a guide to PostgreSQL List Tables. Here we also discuss the Introduction and listing out tables in postgresql and using a select query. You may also have a look at the following articles to learn more –

  1. PostgreSQL LIMIT
  2. Guide to PostgreSQL Primary Key
  3. PostgreSQL Tablespaces
  4. PostgreSQL IF Statement | How to Work?
  5. PostgreSQL Auto Increment | Types
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more