add database name envvar to GetDbUrl

This commit is contained in:
2026-02-09 04:52:44 -08:00
parent 8ba7ac55d6
commit a70dc4882b
2 changed files with 9 additions and 7 deletions

View File

@@ -21,18 +21,23 @@ func CreateAllTables() error {
return CreateSessionsTable()
}
func GetDbUrl() string {
func GetDbUrl(noDBName bool) string {
host := os.Getenv("PGHOST")
port := os.Getenv("PGPORT")
user := os.Getenv("PGUSER")
pass := os.Getenv("PGPASSWORD")
db := os.Getenv("PGDATABASE")
return fmt.Sprintf("postgres://%s:%s@%s:%s", user, pass, host, port)
if noDBName {
return fmt.Sprintf("postgres://%s:%s@%s:%s", user, pass, host, port)
} else {
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, pass, host, port, db)
}
}
func CreateDB() error {
conn, err := pgx.Connect(context.Background(),
GetDbUrl(),
GetDbUrl(true),
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to PostgreSQL: %v\n", err)