Read Connection Service File of PostgreSQL.
This library reads and applies options specified in a section of service file optionally overriden by PGSERVICE and PGSERVICEFILE environment variables.
go get github.com/behrang/go-pgservice
Call Apply function to apply options in service section of file which can be overriden by PGSERVICE and PGSERVICEFILE environment variables. This make them available to pq as environment variables.
package main
import (
"database/sql"
"log"
"github.com/behrang/go-pgservice"
_ "github.com/lib/pq"
)
func main() {
err := pgservice.Apply("service", ".pg_service.conf")
if err != nil {
log.Fatal(err)
}
db, err := sql.Open("postgres", "")
if err != nil {
log.Fatal(err)
}
if err := db.Ping(); err != nil {
log.Fatal(err)
}
}Now running your app with something like PGSERVICE=mydb app will run your app and connect to Postgre database specified in [mydb] section of ~/.pg_service.conf.
host
port
dbname
user
password
sslmode
sslcert
sslkey
sslrootcert