PostgreSQL is a powerful, open-source database system. It is a relational database management system (RDBMS) based on the SQL language. PostgreSQL is one of the most popular database systems in the world. It is the go-to database for many developers and companies.
In this post, I will show you how to list all tables in PostgreSQL using the psql
command-line tool.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
To follow along with this tutorial, you will need:
psql
command-line tool installed on your computer.To connect to the PostgreSQL database, run the following command:
psql -U postgres -h localhost -p 5432
-U
flag specifies the username to connect to the database. In this case, we are connecting to the default postgres
user.-h
flag specifies the hostname of the database server. In this case, we are connecting to the database server running on the same computer, so we use localhost
.-p
flag specifies the port number of the database server. In this case, we are connecting to the default port number 5432
.To check the list of all databases, run the following command:
\l
That is the equivalent of running SHOW DATABASES;
in MySQL.
To switch to a specific database, run the following command:
\c database_name
\c
command is used to switch to a specific database.The \c
command is similar to the USE
command in MySQL.
To list all tables in the database, run the following command:
\dt
\dt
command is used to list all tables in the database in the public schema.The \dt
command is similar to the SHOW TABLES;
command in MySQL.
To list all tables in the database in all schemas, run the following command:
\dt *.*
*.*
specifies that we want to list all tables in all schemas.In this tutorial, you learned how to list all tables in PostgreSQL using the psql
command-line tool.
To learn more about SQL, check out this free SQL eBook:
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.