Skip to content

Instantly share code, notes, and snippets.

@vedashree29296
Created January 29, 2021 08:47
Show Gist options
  • Save vedashree29296/20b2354b5e6ebd0f50465eccb704af01 to your computer and use it in GitHub Desktop.
Save vedashree29296/20b2354b5e6ebd0f50465eccb704af01 to your computer and use it in GitHub Desktop.
Connecting to Postgres
package postgres
import (
"database/sql"
"fmt"
//for connecting to db
_ "github.com/lib/pq"
)
const (
host = "localhost" // replace to IP address, domain name depending on use case
port = 5432
user = "postgres" // replace with username
password = "password" // replace with password
dbname = "postgres" // replace with the database name
)
var (
DbClient *sql.DB
)
func init() {
connectionString := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
var err error
DbClient, err = sql.Open("postgres", connectionString)
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