user973493
user973493

Reputation: 39

How to read schema of a PostgreSQL database

I installed an application that uses a postgreSQL server but I don't know the name of the database and the tables it uses. Is there any command in order to see the name of the database and the tables of this application?

Upvotes: 3

Views: 3279

Answers (3)

leonbloy
leonbloy

Reputation: 76006

Short answer: connect to the default database with psql, and list all databases with '\l' Then, connect to you database of interest, and list tables with '\dt'

Slightly larger answer: A Postgresql server installation usually has a "data directory" (can have more than one, if there are two server instances running, but that's quite unusual), which defines what postgresl calls "a cluster". Inside it, you can have several databases ; you usually have at least the defaults 'template0' and 'template1', plus your own database(s).

Upvotes: 1

A.H.
A.H.

Reputation: 66283

Using the psql on Linux you can use the \l command to list databases, \c dbname to connect to that db and the \d command to list tables in the db.

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359986

If you are able to view the database using the psql terminal command:

> psql -h hostname -U username dbname

...then, in the psql shell, \d ("describe") will show you a list of all the relations in the database. You can use \d on specific relations as well, e.g.

db_name=# \d table_name
        Table "public.table_name"
    Column     | Type    | Modifiers
---------------+---------+-----------
id             | integer | not null
... etc ...

Upvotes: 4

Related Questions