Skip to content

Instantly share code, notes, and snippets.

@heathd
Created January 16, 2015 15:48
Show Gist options
  • Save heathd/dee3bc07829ed2fd100e to your computer and use it in GitHub Desktop.
Save heathd/dee3bc07829ed2fd100e to your computer and use it in GitHub Desktop.
psql connection example
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
)
func main() {
db, err := sql.Open("postgres", "host=/var/run/postgresql/ sslmode=disable dbname=entities")
if err != nil {
fmt.Printf("%v", err)
}
rows, err := db.Query("SELECT * FROM entities")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var num int
if err := rows.Scan(&num); err != nil {
log.Fatal(err)
}
fmt.Printf("num: %v", num)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment