diff --git a/examples/chat/README.md b/examples/chat/README.md index 4b73eb51..4e68df48 100644 --- a/examples/chat/README.md +++ b/examples/chat/README.md @@ -8,12 +8,7 @@ between them. ## Connection configuration -The database connection is configured via the standard PostgreSQL environment variables. - -* PGHOST - defaults to localhost -* PGUSER - defaults to current OS user -* PGPASSWORD - defaults to empty string -* PGDATABASE - defaults to user name +The database connection is configured via DATABASE_URL and standard PostgreSQL environment variables (PGHOST, PGUSER, etc.) You can either export them then run chat: diff --git a/examples/chat/main.go b/examples/chat/main.go index 35b73183..e269b78f 100644 --- a/examples/chat/main.go +++ b/examples/chat/main.go @@ -6,19 +6,14 @@ import ( "fmt" "os" - "github.com/jackc/pgx" + pgxpool "github.com/jackc/pgx/pool" ) -var pool *pgx.ConnPool +var pool *pgxpool.Pool func main() { - 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}) + var err error + pool, err = pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL")) if err != nil { fmt.Fprintln(os.Stderr, "Unable to connect to database:", err) os.Exit(1) @@ -40,7 +35,7 @@ Type "exit" to quit.`) os.Exit(0) } - _, err = pool.Exec("select pg_notify('chat', $1)", msg) + _, err = pool.Exec(context.Background(), "select pg_notify('chat', $1)", msg) if err != nil { fmt.Fprintln(os.Stderr, "Error sending notification:", err) os.Exit(1) @@ -53,22 +48,24 @@ Type "exit" to quit.`) } func listen() { - conn, err := pool.Acquire() + conn, err := pool.Acquire(context.Background()) if err != nil { fmt.Fprintln(os.Stderr, "Error acquiring connection:", err) os.Exit(1) } - defer pool.Release(conn) + defer conn.Release() + + // TODO - determine how listen should be handled in pgx vs. pgconn conn.Exec(context.Background(), "listen chat") - for { - notification, err := conn.WaitForNotification(context.Background()) - if err != nil { - fmt.Fprintln(os.Stderr, "Error waiting for notification:", err) - os.Exit(1) - } + // for { + // notification, err := conn.WaitForNotification(context.Background()) + // if err != nil { + // fmt.Fprintln(os.Stderr, "Error waiting for notification:", err) + // os.Exit(1) + // } - fmt.Println("PID:", notification.PID, "Channel:", notification.Channel, "Payload:", notification.Payload) - } + // fmt.Println("PID:", notification.PID, "Channel:", notification.Channel, "Payload:", notification.Payload) + // } } diff --git a/examples/todo/README.md b/examples/todo/README.md index 32c32aa2..e1d466a3 100644 --- a/examples/todo/README.md +++ b/examples/todo/README.md @@ -19,12 +19,7 @@ Build todo: ## Connection configuration -The database connection is configured via enviroment variables. - -* PGHOST - defaults to localhost -* PGUSER - defaults to current OS user -* PGPASSWORD - defaults to empty string -* PGDATABASE - defaults to user name +The database connection is configured via DATABASE_URL and standard PostgreSQL environment variables (PGHOST, PGUSER, etc.) You can either export them then run todo: diff --git a/examples/todo/main.go b/examples/todo/main.go index 70cbe14c..cbe93307 100644 --- a/examples/todo/main.go +++ b/examples/todo/main.go @@ -1,22 +1,19 @@ package main import ( + "context" "fmt" - "github.com/jackc/pgx" "os" "strconv" + + "github.com/jackc/pgx" ) var conn *pgx.Conn func main() { - config, err := pgx.ParseEnvLibpq() - if err != nil { - fmt.Fprintln(os.Stderr, "Unable to parse environment:", err) - os.Exit(1) - } - - conn, err = pgx.Connect(config) + var err error + conn, err = pgx.Connect(context.Background(), os.Getenv("DATABASE_URL")) if err != nil { fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err) os.Exit(1) @@ -74,7 +71,7 @@ func main() { } func listTasks() error { - rows, _ := conn.Query("select * from tasks") + rows, _ := conn.Query(context.Background(), "select * from tasks") for rows.Next() { var id int32 @@ -90,17 +87,17 @@ func listTasks() error { } func addTask(description string) error { - _, err := conn.Exec("insert into tasks(description) values($1)", description) + _, err := conn.Exec(context.Background(), "insert into tasks(description) values($1)", description) return err } func updateTask(itemNum int32, description string) error { - _, err := conn.Exec("update tasks set description=$1 where id=$2", description, itemNum) + _, err := conn.Exec(context.Background(), "update tasks set description=$1 where id=$2", description, itemNum) return err } func removeTask(itemNum int32) error { - _, err := conn.Exec("delete from tasks where id=$1", itemNum) + _, err := conn.Exec(context.Background(), "delete from tasks where id=$1", itemNum) return err }