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

PostgreSQL - Describe Table

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.

Using psql:

If you are using the psql client:

  1. Connect to the database:

    psql -U your_username -d your_database_name
    
  2. Use the \d command followed by the table name:

    \d your_table_name
    

Using SQL:

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.

Note:

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.

  1. Getting table information in PostgreSQL:

    • Description: Use the \dt command or query the information_schema.tables view.
    • Code:
      -- Using \dt command in psql
      \dt your_table;
      
      -- Querying information_schema
      SELECT *
      FROM information_schema.tables
      WHERE table_name = 'your_table';
      
  2. Using \d command to describe table in PostgreSQL:

    • Description: Utilize the \d command in psql to display table details.
    • Code:
      \d your_table
      
  3. Displaying table structure with pg_table_size in PostgreSQL:

    • Description: Use pg_table_size to get the size of the table.
    • Code:
      SELECT pg_table_size('your_table') AS table_size;
      
  4. Querying information schema for table details in PostgreSQL:

    • Description: Access the information_schema to retrieve detailed information.
    • Code:
      SELECT *
      FROM information_schema.columns
      WHERE table_name = 'your_table';
      
  5. Viewing column details for a table in PostgreSQL:

    • Description: Query the information_schema.columns view to get column information.
    • Code:
      SELECT column_name, data_type, character_maximum_length
      FROM information_schema.columns
      WHERE table_name = 'your_table';
      
  6. Describing indexes and constraints of a table in PostgreSQL:

    • Description: Check indexes and constraints from pg_indexes and pg_constraint.
    • Code:
      -- Indexes
      SELECT *
      FROM pg_indexes
      WHERE tablename = 'your_table';
      
      -- Constraints
      SELECT *
      FROM pg_constraint
      WHERE conrelid = 'your_table'::regclass;
      
  7. Getting statistics and metadata for a table in PostgreSQL:

    • Description: Access statistics and metadata from the pg_stat_all_tables view.
    • Code:
      SELECT *
      FROM pg_stat_all_tables
      WHERE relname = 'your_table';
      
  8. Finding dependencies of a table in PostgreSQL:

    • Description: Identify dependencies using pg_depend and pg_depend_reference.
    • Code:
      SELECT *
      FROM pg_depend
      WHERE objid = 'your_table'::regclass;