diff --git a/examples/chat/README.md b/examples/chat/README.md index a0935255..4b73eb51 100644 --- a/examples/chat/README.md +++ b/examples/chat/README.md @@ -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 diff --git a/examples/chat/main.go b/examples/chat/main.go index 69ef456b..04f56f7c 100644 --- a/examples/chat/main.go +++ b/examples/chat/main.go @@ -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 -} diff --git a/examples/todo/README.md b/examples/todo/README.md index eb3d95ba..32c32aa2 100644 --- a/examples/todo/README.md +++ b/examples/todo/README.md @@ -21,19 +21,19 @@ Build todo: The database connection is configured via enviroment variables. -* TODO_DB_HOST - defaults to localhost -* TODO_DB_USER - defaults to current OS user -* TODO_DB_PASSWORD - defaults to empty string -* TODO_DB_DATABASE - defaults to todo +* 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 todo: - export TODO_DB_HOST=/private/tmp + export PGDATABASE=todo ./todo list Or you can prefix the todo execution with the environment variables: - TODO_DB_HOST=/private/tmp ./todo list + PGDATABASE=todo ./todo list ## Add a todo item @@ -60,7 +60,7 @@ Or you can prefix the todo execution with the environment variables: CREATE TABLE Time: 6.363 ms jack@hk-47~/dev/go/src/github.com/jackc/pgx/examples/todo$ go build - jack@hk-47~/dev/go/src/github.com/jackc/pgx/examples/todo$ export TODO_DB_HOST=/private/tmp + jack@hk-47~/dev/go/src/github.com/jackc/pgx/examples/todo$ export PGDATABASE=todo jack@hk-47~/dev/go/src/github.com/jackc/pgx/examples/todo$ ./todo list jack@hk-47~/dev/go/src/github.com/jackc/pgx/examples/todo$ ./todo add 'Learn Go' jack@hk-47~/dev/go/src/github.com/jackc/pgx/examples/todo$ ./todo list diff --git a/examples/todo/main.go b/examples/todo/main.go index bacdf24e..70cbe14c 100644 --- a/examples/todo/main.go +++ b/examples/todo/main.go @@ -10,8 +10,13 @@ import ( var conn *pgx.Conn func main() { - var err error - conn, err = pgx.Connect(extractConfig()) + config, err := pgx.ParseEnvLibpq() + if err != nil { + fmt.Fprintln(os.Stderr, "Unable to parse environment:", err) + os.Exit(1) + } + + conn, err = pgx.Connect(config) if err != nil { fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err) os.Exit(1) @@ -115,26 +120,3 @@ Example: todo list `) } - -func extractConfig() pgx.ConnConfig { - var config pgx.ConnConfig - - config.Host = os.Getenv("TODO_DB_HOST") - if config.Host == "" { - config.Host = "localhost" - } - - config.User = os.Getenv("TODO_DB_USER") - if config.User == "" { - config.User = os.Getenv("USER") - } - - config.Password = os.Getenv("TODO_DB_PASSWORD") - - config.Database = os.Getenv("TODO_DB_DATABASE") - if config.Database == "" { - config.Database = "todo" - } - - return config -}