your PostgreSQL connection deserves a name: Go edition
An example on how to assign a name to a PostgreSQL connection in Go.
Get it running
- Start the PostgreSQL docker container:
$ docker run --rm \
--publish 5432:5432 \
--name ycdan-postgresql \
--env POSTGRES_USER=postgres \
--env POSTGRES_PASSWORD=postgrespassword \
--detach \
postgres:13.3
- Compile the example program:
$ go build -o your-connection-deserves-a-name
- Start the example program:
$ ./your-connection-deserves-a-name
You should see something like
2021/07/30 19:15:50 Connecting to PostgreSQL on postgres://postgres:postgrespassword@127.0.0.1/postgres
2021/07/30 19:15:50 Connecting to PostgreSQL on postgres://postgres:postgrespassword@127.0.0.1/postgres ... Successful
2021/07/30 19:15:50
2021/07/30 19:15:50 Keeping the connection open ...
2021/07/30 19:15:50 You can connect to the PostgreSQL database and execute the query:
2021/07/30 19:15:50 SELECT pid, usename, application_name, client_addr, state, query, backend_type FROM pg_stat_activity;
2021/07/30 19:15:50 Hit CTRL + C or cancel the process to stop.
2021/07/30 19:15:50
- Login into your database and execute the query:
postgres=# SELECT usename, application_name, client_addr, backend_type FROM pg_stat_activity;
usename | application_name | client_addr | backend_type
----------+-------------------------+-------------+---------------
postgres | currency-conversion-app | 172.17.0.1 | client backend
Don't know what this is all about?
Read the original blog post your database connection deserves a name @ andygrunwald.com.
Additionally, you can check out the projects README.