Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Harkishen-Singh/7d32b51156d140687d0a9508a9295473 to your computer and use it in GitHub Desktop.
Save Harkishen-Singh/7d32b51156d140687d0a9508a9295473 to your computer and use it in GitHub Desktop.
Script to run `\d` against all tables in a schema

Script to describe all tables in a schema

#!/bin/bash

# Set these variables to your actual database connection details
DB_NAME="your_database_name"
DB_USER="your_username"
DB_PASS="your_password"
DB_HOST="localhost"
DB_PORT="5432"

# The schema you're interested in
SCHEMA_NAME="iot_1"

# Use psql to get a list of tables in the schema
TABLES=$(PGPASSWORD=$DB_PASS psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "SELECT tablename FROM pg_tables WHERE schemaname = '$SCHEMA_NAME'")

# Loop over each table and execute the \d command
for TABLE in $TABLES; do
    echo -e "\n\d $SCHEMA_NAME.$TABLE"
    PGPASSWORD=$DB_PASS psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "\d $SCHEMA_NAME.$TABLE"
done

Samples Output

\d iot_1.device
                                        Table "iot_1.device"
  Column  |          Type          | Collation | Nullable |                 Default                  
----------+------------------------+-----------+----------+------------------------------------------
 id       | integer                |           | not null | nextval('iot_1.device_id_seq'::regclass)
 name     | character varying(50)  |           | not null | 
 location | character varying(100) |           | not null | 
Indexes:
    "device_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "iot_1.command" CONSTRAINT "command_device_id_fkey" FOREIGN KEY (device_id) REFERENCES iot_1.device(id)
    TABLE "iot_1.device_status" CONSTRAINT "device_status_device_id_fkey" FOREIGN KEY (device_id) REFERENCES iot_1.device(id)
    TABLE "iot_1.event" CONSTRAINT "event_device_id_fkey" FOREIGN KEY (device_id) REFERENCES iot_1.device(id)
    TABLE "iot_1.humidity_sensor" CONSTRAINT "humidity_sensor_device_id_fkey" FOREIGN KEY (device_id) REFERENCES iot_1.device(id)
    TABLE "iot_1.new_table1" CONSTRAINT "new_table1_device_id_fkey" FOREIGN KEY (device_id) REFERENCES iot_1.device(id)
    TABLE "iot_1.new_table1" CONSTRAINT "new_table1_device_id_fkey1" FOREIGN KEY (device_id) REFERENCES iot_1.device(id)
    TABLE "iot_1.pressure_sensor" CONSTRAINT "pressure_sensor_device_id_fkey" FOREIGN KEY (device_id) REFERENCES iot_1.device(id)
    TABLE "iot_1.sensor" CONSTRAINT "sensor_device_id_fkey" FOREIGN KEY (device_id) REFERENCES iot_1.device(id)
    TABLE "iot_1.temperature_sensor" CONSTRAINT "temperature_sensor_device_id_fkey" FOREIGN KEY (device_id) REFERENCES iot_1.device(id)


\d iot_1.measurement
                                           Table "iot_1.measurement"
   Column    |           Type           | Collation | Nullable |                    Default                    
-------------+--------------------------+-----------+----------+-----------------------------------------------
 id          | integer                  |           | not null | nextval('iot_1.measurement_id_seq'::regclass)
 sensor_id   | integer                  |           | not null | 
 value       | double precision         |           | not null | 
 timestamp   | timestamp with time zone |           | not null | 
 is_archived | boolean                  |           | not null | false
Indexes:
    "measurement_pkey" PRIMARY KEY, btree (id, sensor_id)
    "idx_measurement_sensor_id" btree (sensor_id)
    "idx_measurement_timestamp" btree ("timestamp")
Check constraints:
    "measurement_is_archived_check" CHECK (is_archived = false)
Foreign-key constraints:
    "measurement_sensor_id_fkey" FOREIGN KEY (sensor_id) REFERENCES iot_1.sensor(id)
Triggers:
    trigger_refresh_aggregated_measurement AFTER INSERT OR DELETE OR UPDATE ON iot_1.measurement FOR EACH STATEMENT EXECUTE FUNCTION iot_1.refresh_aggregated_measurement()

... continues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment