I would like to list all tables in the liferay
database in my PostgreSQL install. How do I do that?
I would like to execute SELECT * FROM applications;
in the liferay
database. applications
is a table in my liferay db. How is this done?
Here's a list of all my databases:
postgres=# \list
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
liferay | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | liferay=CTc/postgres
lportal | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
postgres | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
template0 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)
postgres=#
In SQL Query, you can write this code:
Replace your table scheme with YOUR_TABLE_SCHEME;
Example:
To see all scheme and all tables, there is no need of where clause:
This can be used in automation scripts if you don't need all tables in all schemas:
If you wish to list all tables, you must use:
to indicate that you want all tables in all schemas. This will include tables in
pg_catalog
, the system tables, and those ininformation_schema
. There's no built-in way to say "all tables in all user-defined schemas"; you can, however, set yoursearch_path
to a list of all schemas of interest before running\dt
.You may want to do this programmatically, in which case
psql
backslash-commands won't do the job. This is where theINFORMATION_SCHEMA
comes to the rescue. To list tables:BTW, if you ever want to see what
psql
is doing in response to a backslash command, runpsql
with the-E
flag. eg:so you can see that
psql
is searchingpg_catalog.pg_database
when it gets a list of databases. Similarly, for tables within a given database:It's preferable to use the SQL-standard, portable
INFORMATION_SCHEMA
instead of the Pg system catalogs where possible, but sometimes you need Pg-specific information. In those cases it's fine to query the system catalogs directly, andpsql -E
can be a helpful guide for how to do so.To see the public tables you can do
list tables
list table, view, and access privileges
or just the table names
You can type
\?
to get information on all the commands supported in psql.Connect to the database, then list the tables:
That's how I do it anyway.
You can combine those two commands onto a single line, if you prefer: