PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, you can use the \d
command in the psql
command-line client to describe a table (i.e., to see the columns, data types, indexes, constraints, etc., associated with a table). However, this command is specific to psql
and is not a standard SQL command.
psql
:If you are using the psql
client:
Connect to the database:
psql -U your_username -d your_database_name
Use the \d
command followed by the table name:
\d your_table_name
If you want to get table details using SQL, which is more universal and not restricted to the psql
client, you can query the PostgreSQL system catalogs.
For example, to get column details for a table:
SELECT column_name, data_type, is_nullable, column_default FROM information_schema.columns WHERE table_name = 'your_table_name';
This will give you details like column names, data types, whether the column can be null, and any default values.
To retrieve additional table details like indexes, constraints, etc., you'd need to query other system catalog tables or views, such as pg_indexes
, pg_constraints
, and so on.
When querying system catalogs or using psql
commands, ensure you are connected to the correct database and have the necessary permissions to access the details of the table in question.
Getting table information in PostgreSQL:
\dt
command or query the information_schema.tables
view.-- Using \dt command in psql \dt your_table; -- Querying information_schema SELECT * FROM information_schema.tables WHERE table_name = 'your_table';
Using \d command to describe table in PostgreSQL:
\d
command in psql to display table details.\d your_table
Displaying table structure with pg_table_size in PostgreSQL:
pg_table_size
to get the size of the table.SELECT pg_table_size('your_table') AS table_size;
Querying information schema for table details in PostgreSQL:
information_schema
to retrieve detailed information.SELECT * FROM information_schema.columns WHERE table_name = 'your_table';
Viewing column details for a table in PostgreSQL:
information_schema.columns
view to get column information.SELECT column_name, data_type, character_maximum_length FROM information_schema.columns WHERE table_name = 'your_table';
Describing indexes and constraints of a table in PostgreSQL:
pg_indexes
and pg_constraint
.-- Indexes SELECT * FROM pg_indexes WHERE tablename = 'your_table'; -- Constraints SELECT * FROM pg_constraint WHERE conrelid = 'your_table'::regclass;
Getting statistics and metadata for a table in PostgreSQL:
pg_stat_all_tables
view.SELECT * FROM pg_stat_all_tables WHERE relname = 'your_table';
Finding dependencies of a table in PostgreSQL:
pg_depend
and pg_depend_reference
.SELECT * FROM pg_depend WHERE objid = 'your_table'::regclass;