A library that simplifies access to PostgreSQL databases.
- Despite
go-postgres
uses Jack Christensen'sPGX
library internally, it aims to act as a generic database driver likedatabase/sql
and avoid the developer to use specificPGX
types and routines. - Most of the commonly used types in Postgres can be mapped to standard Golang types including
time.Time
for timestamps (except Postgres' time with tz which is not supported) - When reading
JSON/JSONB
fields, the code will try to unmarshall it into the destination variable. In order to just retrieve the json value as a string, add the::text
suffix to the field in theSELECT
query. - To avoid overflows on high
uint64
values, you can store them inNUMERIC(24,0)
fields. - When reading time-only fields, the date part of the
time.Time
variable is set toJanuary 1, 2000
.
package main
import (
"context"
"github.com/randlabs/go-postgres"
)
type Data struct {
Id int
Name string
}
func main() {
ctx := context.Background()
// Create database driver
db, err := postgres.New(ctx, postgres.Options{
Host: "127.0.0.1",
Port: 5432,
User: "postgres",
Password: "1234",
Name: "sampledb",
})
if err != nil {
// ....
}
defer db.Close()
// Insert some data
data := Data{
Id: 1,
Name: "example",
}
_, err = db.Exec(ctx, `INSERT INTO test_table (id, name) VALUES ($1, $2)`, data.Id, data.Name)
if err != nil {
// ....
}
// Read it
var name string
err = db.QueryRow(ctx, `SELECT name FROM test_table WHERE id = $1)`, 1).Scan(&name)
if err != nil {
// ....
if postgres.IsNoRowsError(err) {
// This should not happen. We cannot find the record we just inserted.
}
}
// ....
}
See LICENSE file for details.