A command-line tool to analyze and visualize PostgreSQL database queries with using pev2
PG Explain is a command-line tool to analyze and visualize PostgreSQL database queries. Built with Go and Cobra, it provides an intuitive and structured interface for generating and understanding execution plans, with the ability to visualize them using pev2.
- Command-Oriented: Designed with Cobra for a structured and user-friendly CLI.
- Integration with PostgreSQL: Simplifies database query analysis and visualization.
you can use go install
to add the application to your $GOPATH/bin
:
go install github.com/Rohatsahin/pgexplain@latest
The application relies on PostgreSQL shell environment variables for database connections. Ensure the following environment variables are set:
PGHOST
: PostgreSQL server hostnamePGUSER
: PostgreSQL usernamePGDATABASE
: Target database
You can configure a .pgpass
file for secure password management:
echo "localhost:5432:mydatabase:myuser:mypassword" > ~/.pgpass
chmod 600 ~/.pgpass
For more information on .pgpass
, see the PostgreSQL
official documentation.
Once you've set up the app with commands, you'll need to build it into an executable binary.
go build -o pg_explain
To make your app accessible globally in the terminal, move the binary file to a directory that is included in your system's PATH environment variable.
On Linux or macOS: Standard binary location: /usr/local/bin (you might need to use sudo depending on your system's permissions)
Move the binary:
sudo mv pg_explain /usr/local/bin/
On Windows: For Windows, you can place the binary in a directory such as C:\Program Files\pg_explain\ and then add that directory to your system’s PATH environment variable.
Copy the binary (pg_explain.exe) to a folder like C:\Program Files\pg_explain. Add this folder to your system PATH by: Right-click This PC → Properties → Advanced system settings → Environment Variables. Under System variables, select Path → Edit → New, and add C:\Program Files\pg_explain.
pg_explain
Run the following command to see a list of available commands:
pg_explain --help
pg_explain analyze "SELECT * FROM users;"
pg_explain analyze "SELECT * FROM orders;"
Access the plan from the file system: /.../Plan_Created_on_January_1th_2025_00:00:00.html
copying the file path and opening it to your favorite browser
pg_explain analyze --remote "SELECT * FROM orders;"
Access the plan from the remote URL: https://explain.dalibo.com/plan/..
copying the remote url and opening it to your favorite browser
Use the --help
flag with any command to see its options. For example:
pg_explain analyze --help
This project is licensed under the Apache 2.0 License. See the LICENSE file for details.