Created
June 10, 2020 18:03
-
-
Save hinupurthakur/7fa0263d0529ce3166263b40141a0d4e to your computer and use it in GitHub Desktop.
Postgres connecting to GoLAng
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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