Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

look up table names in PSQL

Tags:

psql

My DB has a lot of tables (Say 400+), and I only remember part of the name of the one I am looking for.

I know \d would show all the tables, but that's too much to look at. Is there some command to list all the tables whose names match the given regex?

Thanks

like image 685
One Two Three Avatar asked Jun 28 '13 21:06

One Two Three


People also ask

How do you find the table name?

Click on the table. Go to Table Tools > Design > Properties > Table Name.

How do I list all databases and tables using psql?

Use \l or \l+ in psql to show all databases in the current PostgreSQL server. Use the SELECT statement to query data from the pg_database to get all databases.

How do I query a table in PostgreSQL?

The PostgreSQL SELECT statement retrieves data from a single or several tables in a database, and returns the data in a result table, called a result-set. Use the SELECT statement to return one or more rows matching the specified criteria from the database tables.


1 Answers

It's built in to psql, you can use wildcards in \d, \dt, etc, eg:

craig=> \dt test*
         List of relations
 Schema |   Name    | Type  | Owner 
--------+-----------+-------+-------
 public | test      | table | craig
 public | testtable | table | craig
 public | testu     | table | craig
 public | testx     | table | craig
(4 rows)

You'll want to use \dt since \d will display details for each table, not just list the table.

You can do this with schemas too, eg:

\dt *.sometable

will list all tables named sometable in any schema.

Much more convenient than writing queries against pg_class joined to pg_namespace, or querying information_schema.

The usual globbing syntax is accepted, where ? is any single character and * is zero or more characters. So \dt ???? would list all tables with four-character names.

Multiple wildcards are permitted, eg:

craig=> \dt public.*e?t*
           List of relations
 Schema |     Name     | Type  | Owner 
--------+--------------+-------+-------
 public | exclude_test | table | craig
 public | prep_test    | table | craig
 public | test         | table | craig
 public | testtable    | table | craig
 public | testu        | table | craig
 public | testx        | table | craig
(6 rows)
like image 55
Craig Ringer Avatar answered Nov 24 '22 06:11

Craig Ringer