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. 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
719
820Create a new directory called ` sqlc-tutorial ` and open it up.
921
@@ -19,17 +31,29 @@ following contents:
1931
2032``` yaml
2133version : " 2"
34+ cloud :
35+ project : " <your project id>"
2236sql :
2337 - engine : " postgresql"
2438 queries : " query.sql"
2539 schema : " schema.sql"
40+ database :
41+ managed : true
2642 gen :
2743 go :
2844 package : " tutorial"
2945 out : " tutorial"
3046 sql_package : " pgx/v5"
3147` ` `
3248
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+
3357sqlc needs to know your database schema and queries in order to generate code.
3458In the same directory, create a file named `schema.sql` with the following
3559content :
@@ -84,6 +108,8 @@ WHERE id = $1
84108RETURNING *;
85109` ` `
86110
111+ # # Generating code
112+
87113You are now ready to generate code. You shouldn't see any output when you run
88114the `generate` subcommand, unless something goes wrong :
89115
@@ -105,6 +131,8 @@ source code. These files comprise a Go package named `tutorial`:
105131 └── query.sql.go
106132` ` `
107133
134+ # # Using generated code
135+
108136You can use your newly-generated `tutorial` package from any Go program.
109137Create a file named `tutorial.go` and add the following contents :
110138
0 commit comments