What does PgHero show you?
PgHero is a small web dashboard that reads PostgreSQL statistics views and presents them as plain findings. It does not install agents or change your data unless you click one of its maintenance buttons.
- Queries: the statements using the most total time, average time and calls, taken from
pg_stat_statements. - Indexes: duplicate indexes, indexes that are never used, and tables read by sequential scans that may need one.
- Space: database, table and index sizes, plus an estimate of bloat.
- Connections: who is connected, from where, and sessions stuck idle inside a transaction.
- Maintenance: when each table was last vacuumed and analyzed, and how close the database is to transaction ID wraparound.
It suits a single application database very well. For long-term graphs across many servers, pair it with a metrics stack such as the one in the Grafana guide.
What do you need first?
A PostgreSQL server you can reach as a superuser, and Docker Engine on the same machine. Running both on one server keeps the database connection on localhost.
psql --version
docker --version
sudo -u postgres psql -c "SELECT version();"If Docker is not installed, follow the Docker Engine tutorial, or deploy the Docker app image from the console. If PostgreSQL itself is new to you, start with the PostgreSQL guide. PgHero itself is light; it needs only tens of megabytes of memory.
How do you enable pg_stat_statements?
Load the module at server start, restart PostgreSQL, and create the extension in the database you want to inspect. Without it PgHero still works, but the query pages stay empty.
Add these lines to postgresql.conf (/etc/postgresql/16/main/ on Ubuntu 24.04, inside the data directory on Rocky and Alma). If shared_preload_libraries already lists something, add to the list instead of replacing it.
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.track = all
pg_stat_statements.max = 10000
track_io_timing = onsudo systemctl restart postgresql
sudo -u postgres psql shop -c "CREATE EXTENSION IF NOT EXISTS pg_stat_statements;"
sudo -u postgres psql shop -c "SELECT calls, round(total_exec_time) AS ms, left(query, 60) FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;"On Rocky and Alma with the project packages the unit is postgresql-17 (or your version). The test query should list a few statements once the application has run for a moment.
How do you create a monitoring role?
Give PgHero its own login that belongs to the built-in pg_monitor role. That role can read every statistics view without owning or changing any table.
sudo -u postgres psql shop <<'SQL'
CREATE ROLE pghero LOGIN;
GRANT pg_monitor TO pghero;
GRANT CONNECT ON DATABASE shop TO pghero;
SQL
sudo -u postgres psql -c "\password pghero"Check that pg_hba.conf allows password logins from localhost (the default host all all 127.0.0.1/32 scram-sha-256 line does). With this role PgHero can read everything it needs, but buttons that run VACUUM or kill queries will be refused, which is a sensible default. Grant more only if you really want those actions from the browser; the PostgreSQL roles guide covers the options.
How do you run PgHero in Docker?
Start the container on the host network so it reaches PostgreSQL on localhost, and protect the page with a username and password.
sudo docker run -d --name pghero --restart unless-stopped --network host -e DATABASE_URL="postgres://pghero:YOUR_PASSWORD@127.0.0.1:5432/shop" -e PGHERO_USERNAME=admin -e PGHERO_PASSWORD="a-long-dashboard-password" ankane/pghero
sudo docker logs --tail 20 pghero
curl -s -o /dev/null -w "%{http_code}\n" -u admin:a-long-dashboard-password http://127.0.0.1:8080/A 200 answer means the app is up and logged in to the database. Special characters in the database password must be percent encoded inside the URL (for example @ becomes %40).
Host networking is chosen on purpose. When Docker publishes a port with -p, it adds its own packet rules that bypass ufw, which surprises many people. With --network host, PgHero listens directly on port 8080 of the server, and your normal firewall decides who can reach it.
How do you keep the dashboard private?
Leave port 8080 closed in the firewall and open the dashboard through an SSH tunnel from your own computer.
# on the server: confirm 8080 is not allowed
sudo ufw status | grep 8080 || echo "8080 closed" # Ubuntu/Debian
sudo firewall-cmd --list-ports # Rocky/Alma
# on your computer
ssh -N -L 8080:127.0.0.1:8080 deploy@198.51.100.24Then browse to http://localhost:8080 on your machine. The traffic travels inside SSH, so the page and its password are never exposed. The remote database access guide explains tunnels in more depth, including keeping one open in the background.
If several people need it, put it behind a reverse proxy with HTTPS and an IP allow list instead, as described in the Caddy HTTPS tutorial, and keep the PgHero password set.
How do you keep query history over time?
pg_stat_statements only holds totals since the last reset, so PgHero can save snapshots into a table for history. Create the table, then run the capture task every few minutes.
sudo -u postgres psql shop <<'SQL'
CREATE TABLE pghero_query_stats (id bigserial PRIMARY KEY, database text, "user" text, query text, query_hash bigint, total_time float, calls bigint, captured_at timestamp);
CREATE INDEX ON pghero_query_stats (database, captured_at);
GRANT SELECT, INSERT, DELETE ON pghero_query_stats TO pghero;
GRANT USAGE ON SEQUENCE pghero_query_stats_id_seq TO pghero;
SQLAdd a root cron entry (sudo crontab -e) that runs the capture inside the existing container:
*/5 * * * * docker exec pghero bin/rake pghero:capture_query_stats >/dev/null 2>&1The query page then offers a time range and per-query charts. Old rows can be pruned with a daily DELETE ... WHERE captured_at < now() - interval '30 days'. The cron jobs guide explains the schedule format.
How do you act on what PgHero finds?
Treat the dashboard as a to-do list: fix the slowest total query first, then review index suggestions one at a time.
- Copy a slow query, run it with
EXPLAIN (ANALYZE, BUFFERS)in psql, and look for sequential scans on large tables. - Create suggested indexes with
CREATE INDEX CONCURRENTLYso writes keep flowing during the build. - Drop an unused index only after a full business cycle, since monthly reports may still need it.
- Sessions idle in transaction hold locks and block vacuum. Fix the application code, and set
idle_in_transaction_session_timeoutas a safety net.
Update PgHero by pulling the image and recreating the container:
sudo docker pull ankane/pghero
sudo docker rm -f pghero
# run the same docker run command againPlans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Database insight with PgHero
Does PgHero slow down my database?
Its own queries read statistics views and are cheap. pg_stat_statements adds a small overhead per query that is acceptable on almost every workload.
Can PgHero watch more than one database?
Yes. Mount a pghero.yml config file listing several databases, each with its own URL, and the dashboard shows a selector.
Why is the queries page empty?
Either pg_stat_statements is not in shared_preload_libraries, the server was not restarted, or the extension was not created in the database PgHero connects to.
Can I install it without Docker?
Yes. Rails applications can add the pghero gem and mount the dashboard inside the app. Docker is simply the quickest route for a standalone server.
Is it safe to open port 8080 to the internet?
It is better not to. The dashboard reveals query text and table names. Use an SSH tunnel or a reverse proxy with HTTPS, a password and an IP allow list.
More in Databases
- Install MySQL 8
- Install MariaDB
- Install PostgreSQL
- Install MongoDB
- PostgreSQL JSON, arrays and special types
- PostgreSQL views, triggers and CTEs
- PostgreSQL full-text search and encryption
- MySQL keys, indexes and full-text search
- MySQL JSON, views and stored procedures
- Apache Solr search server
- Install Redis or Valkey
- Install Memcached
- Apache Cassandra cluster
- Install Apache CouchDB
- InfluxDB for time series
- Install Elasticsearch
- Back up and restore MySQL and MariaDB
- Back up and restore PostgreSQL
- MySQL users, roles and passwords
- PostgreSQL roles and permissions
- Repair a crashed MySQL table
- PostgreSQL streaming replication
- MongoDB replica set
- PostGIS for location data
- Couchbase Server
- ArangoDB multi-model database
- Remote database access over SSH