11# Getting started with PostgreSQL
22
33This tutorial assumes that the latest version of sqlc is
4- [ installed] ( ../overview/install.md ) and ready to use.
4+ [ installed] ( ../overview/install.md ) and ready to use. And we'll
5+ be generating Go code, but other
6+ [ language plugins] ( ../reference/language-support.rst ) are available.
57
68Create a new directory called ` sqlc-tutorial ` and open it up.
79
8- Initialize a new Go module named ` tutorial.sqlc.dev/app `
10+ Initialize a new Go module named ` tutorial.sqlc.dev/app ` :
911
1012``` shell
1113go mod init tutorial.sqlc.dev/app
2527 go :
2628 package : " tutorial"
2729 out : " tutorial"
30+ sql_package : " pgx/v5"
2831` ` `
2932
3033sqlc needs to know your database schema and queries in order to generate code.
@@ -39,7 +42,7 @@ CREATE TABLE authors (
3942);
4043` ` `
4144
42- Next, create a `query.sql` file with the following four queries :
45+ Next, create a `query.sql` file with the following five queries :
4346
4447` ` ` sql
4548-- name: GetAuthor :one
@@ -58,23 +61,19 @@ INSERT INTO authors (
5861)
5962RETURNING *;
6063
61- -- name: DeleteAuthor :exec
62- DELETE FROM authors
63- WHERE id = $1;
64- ` ` `
65-
66- If you **do not** want your SQL `UPDATE` queries to return the updated record
67- to the user, add this to `query.sql` :
68-
69- ` ` ` sql
7064-- name: UpdateAuthor :exec
7165UPDATE authors
7266 set name = $2,
7367 bio = $3
7468WHERE id = $1;
69+
70+ -- name: DeleteAuthor :exec
71+ DELETE FROM authors
72+ WHERE id = $1;
7573` ` `
7674
77- Otherwise, to return the updated record to the user, add this to `query.sql` :
75+ If you prefer, you can alter the `UpdateAuthor` query to return the updated
76+ record :
7877
7978` ` ` sql
8079-- name: UpdateAuthor :one
@@ -85,13 +84,15 @@ WHERE id = $1
8584RETURNING *;
8685` ` `
8786
88- You are now ready to generate code. You shouldn't see any errors or output.
87+ You are now ready to generate code. You shouldn't see any output when you run
88+ the `generate` subcommand, unless something goes wrong :
8989
9090` ` ` shell
9191sqlc generate
9292` ` `
9393
94- You should now have a `tutorial` package containing three files.
94+ You should now have a `tutorial` subdirectory with three files containing Go
95+ source code. These files comprise a Go package named `tutorial` :
9596
9697` ` `
9798├── go.mod
@@ -104,31 +105,33 @@ You should now have a `tutorial` package containing three files.
104105 └── query.sql.go
105106` ` `
106107
107- You can use your newly generated queries in `app.go`.
108+ You can use your newly-generated `tutorial` package from any Go program.
109+ Create a file named `tutorial.go` and add the following contents :
108110
109111` ` ` go
110112package main
111113
112114import (
113115 "context"
114- "database/sql"
115116 "log"
116117 "reflect"
117118
118- "tutorial.sqlc.dev/app/tutorial"
119+ "github.com/jackc/pgx/v5"
120+ "github.com/jackc/pgx/v5/pgtype"
119121
120- _ "github.com/lib/pq "
122+ "tutorial.sqlc.dev/app/tutorial "
121123)
122124
123125func run() error {
124126 ctx := context.Background()
125127
126- db , err := sql.Open("postgres" , "user=pqgotest dbname=pqgotest sslmode=verify-full")
128+ conn , err := pgx.Connect(ctx , "user=pqgotest dbname=pqgotest sslmode=verify-full")
127129 if err != nil {
128130 return err
129131 }
132+ defer conn.Close(ctx)
130133
131- queries := tutorial.New(db )
134+ queries := tutorial.New(conn )
132135
133136 // list all authors
134137 authors, err := queries.ListAuthors(ctx)
@@ -140,7 +143,7 @@ func run() error {
140143 // create an author
141144 insertedAuthor, err := queries.CreateAuthor(ctx, tutorial.CreateAuthorParams{
142145 Name: "Brian Kernighan",
143- Bio: sql.NullString {String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
146+ Bio: pgtype.Text {String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
144147 })
145148 if err != nil {
146149 return err
@@ -165,13 +168,23 @@ func main() {
165168}
166169` ` `
167170
168- Before the code will compile, you'll need to add the Go PostgreSQL driver.
171+ Before this code will compile you'll need to fetch the relevant PostgreSQL
172+ driver. You can use `lib/pq` with the standard library's `database/sql`
173+ package, but in this tutorial we've used `pgx/v5` :
169174
170- ```
171- go get github.com/lib/pq
175+ ` ` ` shell
176+ go get github.com/jackc/pgx/v5
172177go build ./...
173178` ` `
174179
175- sqlc generates readable, ** idiomatic** Go code that you otherwise would have
176- had to write yourself. Take a look in the ` tutorial ` package to see what code
177- sqlc generated.
180+ The program should compile without errors. To make that possible, sqlc generates
181+ readable, **idiomatic** Go code that you otherwise would've had to write
182+ yourself. Take a look in `tutorial/query.sql.go`.
183+
184+ Of course for this program to run successfully you'll need
185+ to compile after replacing the database connection parameters in the call to
186+ ` pgx.Connect()` with the correct parameters for your database. And your
187+ database must have the `authors` table as defined in `schema.sql`.
188+
189+ You should now have a working program using sqlc's generated Go source code,
190+ and hopefully can see how you'd use sqlc in your own real-world applications.
0 commit comments