go

command module
v0.0.0-...-64d6a9b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 5 Imported by: 0

README

PostgreSQL logo

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

  1. 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
  1. Compile the example program:
$ go build -o your-connection-deserves-a-name
  1. 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
  1. 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.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL