|
| 1 | +================ |
| 2 | +Using Go and pgx |
| 3 | +================ |
| 4 | + |
| 5 | +.. note:: |
| 6 | + Experimental support for :code:`pgx/v5` was added in v1.17.2. Full support will be |
| 7 | + included in v1.18.0. Until then, you'll need to pass the :code:`--experimental` |
| 8 | + flag to :code:`sqlc generate`. |
| 9 | + |
| 10 | + |
| 11 | +pgx is a pure Go driver and toolkit for PostgreSQL. It's become the default |
| 12 | +PostgreSQL package for many Gophers since lib/pq was put into maitience mode. |
| 13 | + |
| 14 | +^^^^^^^^^^^^^^^ |
| 15 | +Getting started |
| 16 | +^^^^^^^^^^^^^^^ |
| 17 | + |
| 18 | +To start generating code that uses pgx, set the :code:`sql_package` field in |
| 19 | +your :code:`sqlc.yaml` configuration file. Valid options are :code:`pgx/v4` or |
| 20 | +:code:`pgx/v5` |
| 21 | + |
| 22 | +.. code-block:: yaml |
| 23 | +
|
| 24 | + version: "2" |
| 25 | + sql: |
| 26 | + - engine: "postgresql" |
| 27 | + queries: "query.sql" |
| 28 | + schema: "query.sql" |
| 29 | + gen: |
| 30 | + go: |
| 31 | + package: "db" |
| 32 | + sql_package: "pgx/v5" |
| 33 | + out: "db" |
| 34 | +
|
| 35 | +If you don't have an existing sqlc project on hand, create a directory with the |
| 36 | +configuration file above and the following :code:`query.sql` file. |
| 37 | + |
| 38 | +.. code-block:: sql |
| 39 | +
|
| 40 | + CREATE TABLE authors ( |
| 41 | + id BIGSERIAL PRIMARY KEY, |
| 42 | + name text NOT NULL, |
| 43 | + bio text |
| 44 | + ); |
| 45 | +
|
| 46 | + -- name: GetAuthor :one |
| 47 | + SELECT * FROM authors |
| 48 | + WHERE id = $1 LIMIT 1; |
| 49 | + |
| 50 | + -- name: ListAuthors :many |
| 51 | + SELECT * FROM authors |
| 52 | + ORDER BY name; |
| 53 | + |
| 54 | + -- name: CreateAuthor :one |
| 55 | + INSERT INTO authors ( |
| 56 | + name, bio |
| 57 | + ) VALUES ( |
| 58 | + $1, $2 |
| 59 | + ) |
| 60 | + RETURNING *; |
| 61 | + |
| 62 | + -- name: DeleteAuthor :exec |
| 63 | + DELETE FROM authors |
| 64 | + WHERE id = $1; |
| 65 | +
|
| 66 | +
|
| 67 | +Generating the code will now give you pgx-compatible database access methods. |
| 68 | + |
| 69 | +.. code-block:: bash |
| 70 | +
|
| 71 | + sqlc generate --experimental |
| 72 | +
|
| 73 | +^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 74 | +Generated code walkthrough |
| 75 | +^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 76 | + |
| 77 | +The generated code is very similar to the code generated when using |
| 78 | +:code:`lib/pq`. However, instead of using :code:`database/sql`, the code uses |
| 79 | +pgx types directly. |
| 80 | + |
| 81 | +.. code-block:: go |
| 82 | +
|
| 83 | + package main |
| 84 | + |
| 85 | + import ( |
| 86 | + "context" |
| 87 | + "fmt" |
| 88 | + "os" |
| 89 | + |
| 90 | + "github.com/jackc/pgx/v5" |
| 91 | + |
| 92 | + "example.com/sqlc-tutorial/db" |
| 93 | + ) |
| 94 | + |
| 95 | + func main() { |
| 96 | + // urlExample := "postgres://username:password@localhost:5432/database_name" |
| 97 | + conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL")) |
| 98 | + if err != nil { |
| 99 | + fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err) |
| 100 | + os.Exit(1) |
| 101 | + } |
| 102 | + defer conn.Close(context.Background()) |
| 103 | +
|
| 104 | + q := db.New(conn) |
| 105 | + |
| 106 | + author, err := q.GetAuthor(context.Background(), 1) |
| 107 | + if err != nil { |
| 108 | + fmt.Fprintf(os.Stderr, "GetAuthor failed: %v\n", err) |
| 109 | + os.Exit(1) |
| 110 | + } |
| 111 | + |
| 112 | + fmt.Println(author.Name) |
| 113 | + } |
0 commit comments