Skip to content

Commit 449becf

Browse files
committed
updated the username password in such a way that it will always take it from the environment variables
1 parent 6e6853a commit 449becf

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

connect_with_a_database/main.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ import (
2525
*/
2626

2727
func connectHandler() {
28-
dbUrl := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", "localhost", 5432, "<your_username>", "<your_pass>", "postgres")
28+
29+
env := os.Environ()
30+
31+
for _, val := range env {
32+
fmt.Println(val)
33+
}
34+
dbUrl := fmt.Sprintf(
35+
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", "localhost", 5432, os.Getenv("PG_USERNAME_V1"), os.Getenv("PG_PASS_V1"), "postgres",
36+
)
2937

3038
os.Setenv("DATABASE_CONFIG", dbUrl)
3139

@@ -40,7 +48,7 @@ func connectHandler() {
4048

4149
var greeting string
4250

43-
err = connection.QueryRow(context.Background(), "select 'Hello World'").Scan(&greeting)
51+
err = connection.QueryRow(context.Background(), "select name from dummy_table;").Scan(&greeting)
4452

4553
if err != nil {
4654
fmt.Fprintf(os.Stderr, "Failed to query row %v", err)

0 commit comments

Comments
 (0)