1
1
# Getting started with PostgreSQL
2
2
3
3
This tutorial assumes that the latest version of sqlc is
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.
4
+ [ installed] ( ../overview/install.md ) and ready to use.
5
+
6
+ We'll generate Go code here, but other
7
+ [ language plugins] ( ../reference/language-support.rst ) are available. You'll
8
+ naturally need the Go toolchain if you want to build and run a program with the
9
+ code sqlc generates, but sqlc itself has no dependencies.
10
+
11
+ We'll also rely on sqlc's [ managed databases] ( ../howto/managed-databases.md ) ,
12
+ which require a sqlc Cloud project and auth token. You can get those from
13
+ the [ sqlc Cloud dashboard] ( https://dashboard.sqlc.dev/ ) . Managed databases are
14
+ an optional feature that improves sqlc's query analysis in many cases, but you
15
+ can turn it off simply by removing the ` cloud ` and ` database ` sections of your
16
+ configuration.
17
+
18
+ ## Setting up
7
19
8
20
Create a new directory called ` sqlc-tutorial ` and open it up.
9
21
@@ -19,17 +31,29 @@ following contents:
19
31
20
32
``` yaml
21
33
version : " 2"
34
+ cloud :
35
+ project : " <your project id>"
22
36
sql :
23
37
- engine : " postgresql"
24
38
queries : " query.sql"
25
39
schema : " schema.sql"
40
+ database :
41
+ managed : true
26
42
gen :
27
43
go :
28
44
package : " tutorial"
29
45
out : " tutorial"
30
46
sql_package : " pgx/v5"
31
47
` ` `
32
48
49
+ And finally, set the ` SQLC_AUTH_TOKEN` environment variable:
50
+
51
+ ` ` ` shell
52
+ export SQLC_AUTH_TOKEN="<your sqlc auth token>"
53
+ ` ` `
54
+
55
+ # # Schema and queries
56
+
33
57
sqlc needs to know your database schema and queries in order to generate code.
34
58
In the same directory, create a file named `schema.sql` with the following
35
59
content :
@@ -84,6 +108,8 @@ WHERE id = $1
84
108
RETURNING *;
85
109
` ` `
86
110
111
+ # # Generating code
112
+
87
113
You are now ready to generate code. You shouldn't see any output when you run
88
114
the `generate` subcommand, unless something goes wrong :
89
115
@@ -105,6 +131,8 @@ source code. These files comprise a Go package named `tutorial`:
105
131
└── query.sql.go
106
132
` ` `
107
133
134
+ # # Using generated code
135
+
108
136
You can use your newly-generated `tutorial` package from any Go program.
109
137
Create a file named `tutorial.go` and add the following contents :
110
138
0 commit comments