Skip to content

Instantly share code, notes, and snippets.

@aodin aodin/gist:9493340
Created Mar 11, 2014

Embed
What would you like to do?
Insert to PostGres
package main
import (
"database/sql"
"log"
_ "github.com/lib/pq"
)
func connect(driver string, credentials string) *sql.DB {
db, connectErr := sql.Open(driver, credentials)
if connectErr != nil {
panic("Could not connect to database")
}
return db
}
func insert(conn *sql.DB) {
insertSQL := `INSERT INTO users (name, password) VALUES ($1, $2)`
result, execErr := conn.Exec(insertSQL, "admin", "secret")
if execErr != nil {
log.Println("Exec err:", execErr)
}
log.Printf("Result: %+v\n", result)
}
func main() {
db := connect("postgres", "host=localhost port=5432 dbname=aspect user=postgres password=gotest")
defer db.Close()
insert(db)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.