Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Go postgres connect to database
import "fmt"
import "os"
import "log"
import (
"database/sql"
_ "github.com/lib/pq"
)
func main() {
connStr := "user=postgres password=changeme dbname=database sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
rows, err := db.Query("SELECT NOW()")
if err != nil{
fmt.Println("pq error:", err)
os.Exit(1)
}
_ = rows
fmt.Println("Connect OK")
os.Exit(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment