The recommended way to connect to postgres is via the client pq.
Then you can either use the standard database/sql inteface. Or you can use the lightweight ORM jet.
//This example uses the ORM jet
package main
import (
"github.com/eaigner/jet"
"github.com/lib/pq"
"log"
"os"
)
func logFatal(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
//Make sure you setup the ELEPHANTSQL_URL to be a uri, e.g. 'postgres://user:pass@host/db?options'
pgUrl, err := pq.ParseURL(os.Getenv("ELEPHANTSQL_URL"))
logFatal(err)
db, err := jet.Open("postgres", pgUrl)
logFatal(err)
var people []*struct {
Id int
FirstName string
LastName string
}
err = db.Query("SELECT * FROM people").Rows(&people)
logFatal(err)
for _, person := range people {
log.Printf("Id: %v, First Name: %s, Last Name: %s",
person.Id,
person.FirstName,
person.LastName)
}
}