pgservice

package module
v0.0.0-...-0995c0d Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2017 License: MIT Imports: 3 Imported by: 0

README

go-pgservice

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.

Install
go get github.com/behrang/go-pgservice
Usage

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.

Supported Options In Connection Service File
host
port
dbname
user
password
sslmode
sslcert
sslkey
sslrootcert
License

MIT

Documentation

Overview

Support PGSERVICE and PGSERVICEFILE environment variables for PostgreSQL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(service, file string) error

Apply config options specified in `service` section of `file` (defaults to `~/.pg_service.conf`) to environment variables. This make them available to [`pq`](https://github.com/lib/pq). It also removes `PGSERVICE` and `PGSERVICEFILE` from environment variables to prevent `pq` panic. Read more about [The Connection Service File](https://www.postgresql.org/docs/current/static/libpq-pgservice.html).

func Read

func Read(service, file string) (map[string]string, error)

Reads options specified in a section of a file and returns them as a map.

Types

This section is empty.

Jump to

Keyboard shortcuts

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