pgpass

package module
v0.0.0-...-a55aa6d Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2020 License: MIT Imports: 8 Imported by: 0

README

pgpass GoDoc Build Status

Access PostreSQL password file as described in libpq documentation. This libarary will allow you to iterate all the entries in pgpass file as well as easily access passwords for a given host and username pair.

Default file

When applicable, the default pgpass file being used is read from ~/.pgpass location. This is not going to work on Windows, I uderstand, but I don't own one and cannot test. Appropriate patch reading from %APPDATA%\postgresql\pgpass.conf should be quite trivial though.

Examples

Get password
// Input:
// localhost:5432:db:tg:letmein
// *:2345:db:tg:trustno1
// *:*:db:*:superman

package main

import (
	"fmt"

	"github.com/tg/pgpass"
)

func main() {
	for _, host := range []string{"localhost", "remotehost:2345", "spacehost"} {
		pass, err := pgpass.Password(host, "tg")
		if err != nil {
			panic(err)
		}		
		fmt.Println(pass)		
	}
}

// Output:
// letmein
// trustno1
// superman
Inject password into an URL
package main

import (
	"fmt"

	"github.com/tg/pgpass"
)

func main() {
	u, _ := pgpass.UpdateURL("postgres://tg@localhost:5432/db?sslmode=disable")
	fmt.Println(u) // postgres://tg:letmein@localhost:5432/db?sslmode=disable
	// Now you can call sql.Open("postgres", u)
}

Why?

Because my password is in ~/.pgpass already, for use with psql. Because I don't want to duplicate it in the config file for each process. Because someone will send the config file to his mum by email one day, or store it in github.

Documentation

Overview

Package pgpass allows for iterating all entries in pgpass (postgresql password file) file as well as easily accessing passwords for a given host and username.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotEnoughFields = errors.New("Not enough fields")

ErrNotEnoughFields indicates line doesn't contain enough fields

Functions

func OpenDefault

func OpenDefault() (f *os.File, err error)

OpenDefault opens default pgpass file, which is ~/.pgpass. Current homedir will be retrieved by calling user.Current or using $HOME on failure.

func Password

func Password(host, user string) (string, error)

Password reads password for given host and user from a default pgpass file. Host should be of the form "hostname:port".

func PasswordFrom

func PasswordFrom(host, user string, r io.Reader) (string, error)

PasswordFrom reads password for given host and user from r, which should be in a valid pgpass format. Host should be of the form "hostname:port".

func UpdateURL

func UpdateURL(dburl string) (string, error)

UpdateURL injects password into URL if not already provided. Password will be loaded from the default pgpass file.

Types

type Entry

type Entry struct {
	Hostname, Port, Database, Username, Password string
}

Entry represents single entry in pgpass file

type EntryReader

type EntryReader struct {
	// contains filtered or unexported fields
}

EntryReader reads entries from pgpass file

func NewEntryReader

func NewEntryReader(r io.Reader) *EntryReader

NewEntryReader returns new entry reader from provided r

func (*EntryReader) Entry

func (er *EntryReader) Entry() Entry

Entry returns last read entry

func (*EntryReader) Err

func (er *EntryReader) Err() error

Err returns underlying error if any. Should be checked after unsuccessful call to Next().

func (*EntryReader) Next

func (er *EntryReader) Next() (ok bool)

Next reads in next entry and returns true on success

Jump to

Keyboard shortcuts

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