| 41 | } |
| 42 | |
| 43 | func Example_iam() { |
| 44 | // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 45 | // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/postgres/awspostgres" |
| 46 | // PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 47 | ctx := context.Background() |
| 48 | |
| 49 | // To use IAM authentication, omit the password from the URL. |
| 50 | // The IAM user or role must be granted rds_iam permission in the database. |
| 51 | db, err := postgres.Open(ctx, |
| 52 | "awspostgres://iamuser@example01.xyzzy.us-west-1.rds.amazonaws.com:5432/testdb") |
| 53 | if err != nil { |
| 54 | log.Fatal(err) |
| 55 | } |
| 56 | defer db.Close() |
| 57 | |
| 58 | // Use database in your program. |
| 59 | if _, err := db.ExecContext(ctx, "CREATE TABLE IF NOT EXISTS foo (bar INT)"); err != nil { |
| 60 | log.Fatal(err) |
| 61 | } |
| 62 | if _, err := db.ExecContext(ctx, "INSERT INTO foo (bar) VALUES ($1)", 42); err != nil { |
| 63 | log.Fatal(err) |
| 64 | } |
| 65 | var val int |
| 66 | if err := db.QueryRowContext(ctx, "SELECT bar FROM foo LIMIT 1").Scan(&val); err != nil { |
| 67 | log.Fatal(err) |
| 68 | } |
| 69 | } |