A Ruby script to export PostgreSQL table statistics and metrics to a YAML file.
- Connects to PostgreSQL using a connection URI from environment variables (supports
.env
files) - Lists all user tables
- Exports per-table stats including:
- Estimated row count
- Read queries per second (QPS)
- Write queries per second (QPS)
- Various table-level counters
- Outputs results in a YAML file
-
Clone this repository or copy the script files.
-
Install dependencies:
bundle install
-
Create a
.env
file in the project root with your database connection string:DB_URI=postgres://user:password@host:port/dbname # Optionally override output file name # YAML_FILE=table_stats.yaml
-
Run the script:
ruby export_postgres_stats.rb
The output YAML file (default: table_stats.yaml
) will contain stats for each table in the following format:
table_name:
row_count: 1234
read_qps: 0.0123
write_qps: 0.0004
seq_scan: 10
...