Update examples to use PG envvars
This commit is contained in:
@@ -8,18 +8,18 @@ between them.
|
|||||||
|
|
||||||
## Connection configuration
|
## 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
|
* PGHOST - defaults to localhost
|
||||||
* CHAT_DB_USER - defaults to current OS user
|
* PGUSER - defaults to current OS user
|
||||||
* CHAT_DB_PASSWORD - defaults to empty string
|
* PGPASSWORD - defaults to empty string
|
||||||
* CHAT_DB_DATABASE - defaults to postgres
|
* PGDATABASE - defaults to user name
|
||||||
|
|
||||||
You can either export them then run chat:
|
You can either export them then run chat:
|
||||||
|
|
||||||
export CHAT_DB_HOST=/private/tmp
|
export PGHOST=/private/tmp
|
||||||
./chat
|
./chat
|
||||||
|
|
||||||
Or you can prefix the chat execution with the environment variables:
|
Or you can prefix the chat execution with the environment variables:
|
||||||
|
|
||||||
CHAT_DB_HOST=/private/tmp ./chat
|
PGHOST=/private/tmp ./chat
|
||||||
|
|||||||
+7
-25
@@ -12,8 +12,13 @@ import (
|
|||||||
var pool *pgx.ConnPool
|
var pool *pgx.ConnPool
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
config, err := pgx.ParseEnvLibpq()
|
||||||
pool, err = pgx.NewConnPool(extractConfig())
|
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 {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "Unable to connect to database:", err)
|
fmt.Fprintln(os.Stderr, "Unable to connect to database:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -68,26 +73,3 @@ func listen() {
|
|||||||
fmt.Println("PID:", notification.PID, "Channel:", notification.Channel, "Payload:", notification.Payload)
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -21,19 +21,19 @@ Build todo:
|
|||||||
|
|
||||||
The database connection is configured via enviroment variables.
|
The database connection is configured via enviroment variables.
|
||||||
|
|
||||||
* TODO_DB_HOST - defaults to localhost
|
* PGHOST - defaults to localhost
|
||||||
* TODO_DB_USER - defaults to current OS user
|
* PGUSER - defaults to current OS user
|
||||||
* TODO_DB_PASSWORD - defaults to empty string
|
* PGPASSWORD - defaults to empty string
|
||||||
* TODO_DB_DATABASE - defaults to todo
|
* PGDATABASE - defaults to user name
|
||||||
|
|
||||||
You can either export them then run todo:
|
You can either export them then run todo:
|
||||||
|
|
||||||
export TODO_DB_HOST=/private/tmp
|
export PGDATABASE=todo
|
||||||
./todo list
|
./todo list
|
||||||
|
|
||||||
Or you can prefix the todo execution with the environment variables:
|
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
|
## Add a todo item
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ Or you can prefix the todo execution with the environment variables:
|
|||||||
CREATE TABLE
|
CREATE TABLE
|
||||||
Time: 6.363 ms
|
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$ 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 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 add 'Learn Go'
|
||||||
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 list
|
||||||
|
|||||||
+7
-25
@@ -10,8 +10,13 @@ import (
|
|||||||
var conn *pgx.Conn
|
var conn *pgx.Conn
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
config, err := pgx.ParseEnvLibpq()
|
||||||
conn, err = pgx.Connect(extractConfig())
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "Unable to parse environment:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err = pgx.Connect(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -115,26 +120,3 @@ Example:
|
|||||||
todo list
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user