2
0

Update examples to use PG envvars

This commit is contained in:
Jack Christensen
2017-09-01 14:24:55 -05:00
parent d0d4002ecf
commit e2695be13b
4 changed files with 28 additions and 64 deletions
+7 -7
View File
@@ -8,18 +8,18 @@ between them.
## Connection configuration
The database connection is configured via enviroment variables.
The database connection is configured via the standard PostgreSQL environment variables.
* CHAT_DB_HOST - defaults to localhost
* CHAT_DB_USER - defaults to current OS user
* CHAT_DB_PASSWORD - defaults to empty string
* CHAT_DB_DATABASE - defaults to postgres
* PGHOST - defaults to localhost
* PGUSER - defaults to current OS user
* PGPASSWORD - defaults to empty string
* PGDATABASE - defaults to user name
You can either export them then run chat:
export CHAT_DB_HOST=/private/tmp
export PGHOST=/private/tmp
./chat
Or you can prefix the chat execution with the environment variables:
CHAT_DB_HOST=/private/tmp ./chat
PGHOST=/private/tmp ./chat
+7 -25
View File
@@ -12,8 +12,13 @@ import (
var pool *pgx.ConnPool
func main() {
var err error
pool, err = pgx.NewConnPool(extractConfig())
config, err := pgx.ParseEnvLibpq()
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to parse environment:", err)
os.Exit(1)
}
pool, err = pgx.NewConnPool(pgx.ConnPoolConfig{ConnConfig: config})
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to connect to database:", err)
os.Exit(1)
@@ -68,26 +73,3 @@ func listen() {
fmt.Println("PID:", notification.PID, "Channel:", notification.Channel, "Payload:", notification.Payload)
}
}
func extractConfig() pgx.ConnPoolConfig {
var config pgx.ConnPoolConfig
config.Host = os.Getenv("CHAT_DB_HOST")
if config.Host == "" {
config.Host = "localhost"
}
config.User = os.Getenv("CHAT_DB_USER")
if config.User == "" {
config.User = os.Getenv("USER")
}
config.Password = os.Getenv("CHAT_DB_PASSWORD")
config.Database = os.Getenv("CHAT_DB_DATABASE")
if config.Database == "" {
config.Database = "postgres"
}
return config
}