set up connection to DB
This commit is contained in:
parent
eea30c7318
commit
ee07974060
|
@ -10,20 +10,17 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// urlExample := "postgres://username:password@localhost:5432/database_name"
|
// urlExample := "postgres://username:password@localhost:5432/database_name"
|
||||||
conn, err := pgx.Connect(context.Background(), os.Getenv("PGHOST"))
|
host := os.Getenv("PGHOST")
|
||||||
|
database := os.Getenv("DB_NAME")
|
||||||
|
|
||||||
|
connectString :=
|
||||||
|
fmt.Sprintf("postgres:///?host=%s&database=%s", host, database)
|
||||||
|
|
||||||
|
conn, err := pgx.Connect(context.Background(), connectString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer conn.Close(context.Background())
|
defer conn.Close(context.Background())
|
||||||
|
|
||||||
var name string
|
|
||||||
var weight int64
|
|
||||||
err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(name, weight)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue