Skip to content

Instantly share code, notes, and snippets.

@hinupurthakur
Created June 10, 2020 18:03
Show Gist options
  • Save hinupurthakur/7fa0263d0529ce3166263b40141a0d4e to your computer and use it in GitHub Desktop.
Save hinupurthakur/7fa0263d0529ce3166263b40141a0d4e to your computer and use it in GitHub Desktop.
Postgres connecting to GoLAng
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "nupur"
password = "q"
dbname = "test"
)
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment