Saturday 18 May 2019

Postgres: DESCRIBE table


Postgres do not have ‘DESCRIBE’ command. But there are couple of ways to get information about a table.

Using \d {tableName}
Connect to the database and execute the command ‘\d employee’ to get information about employee.

$psql -U postgres -h localhost --dbname=myOrg
Password for user postgres: 
psql (14.4)
Type "help" for help.

myOrg=# 
myOrg=# \d
          List of relations
 Schema |   Name   | Type  |  Owner   
--------+----------+-------+----------
 public | employee | table | postgres
(1 row)

myOrg=# 
myOrg=# \d employee
                     Table "public.employee"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 id     | integer               |           |          | 
 name   | character varying(20) |           |          | 

 
By querying information_schema.COLUMNS table
Example
SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = 'employee';

SELECT column_name FROM information_schema.COLUMNS WHERE TABLE_NAME = 'employee';


SELECT column_name, data_type, collation_name, is_nullable,column_default FROM information_schema.COLUMNS WHERE TABLE_NAME = 'employee';
myOrg=# SELECT column_name, data_type, collation_name, is_nullable,column_default FROM information_schema.COLUMNS WHERE TABLE_NAME = 'employee';
 column_name |     data_type     | collation_name | is_nullable | column_default 
-------------+-------------------+----------------+-------------+----------------
 id          | integer           |                | YES         | 
 name        | character varying |                | YES         | 
(2 rows)




Previous                                                 Next                                                 Home

No comments:

Post a Comment