Skip to content

Instantly share code, notes, and snippets.

@chrisjsimpson
Created July 25, 2021 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisjsimpson/0312a6439739cab3ffcc22a398de8548 to your computer and use it in GitHub Desktop.
Save chrisjsimpson/0312a6439739cab3ffcc22a398de8548 to your computer and use it in GitHub Desktop.
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