Created
January 29, 2021 08:47
-
-
Save vedashree29296/20b2354b5e6ebd0f50465eccb704af01 to your computer and use it in GitHub Desktop.
Connecting to Postgres
This file contains 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 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