11# Managed databases
22
3- ``` {note}
4- Managed databases are powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign up for [free](https://dashboard.sqlc.dev) today.
5- ```
6-
73* Added in v1.22.0*
84
9- ` sqlc ` can create and maintain short-lived hosted databases for your project.
10- These ephemeral databases are immediately useful for powering sqlc's
11- database-connected query analyzer, an opt-in feature that improves upon sqlc's
12- built-in query analysis engine. PostgreSQL support is available today, with
13- MySQL on the way.
5+ ` sqlc ` can automatically create read-only databases to power query analysis,
6+ linting and verification. These databases are immediately useful for powering
7+ sqlc's database-connected query analyzer, an opt-in feature that improves upon
8+ sqlc's built-in query analysis engine. PostgreSQL support is available today,
9+ with MySQL on the way.
1410
1511Once configured, ` sqlc ` will also use managed databases when linting queries
1612with [ ` sqlc vet ` ] ( vet.md ) in cases where your lint rules require a connection
1713to a running database.
1814
1915Managed databases are under active development, and we're interested in
20- supporting other use-cases. Outside of sqlc itself, you can use our managed
21- databases in your tests to quickly stand up a database per test suite or even per test,
22- providing a real, isolated database for a test run. No cleanup required.
16+ supporting other use-cases.
2317
2418## Configuring managed databases
2519
2620To configure ` sqlc ` to use managed databases, remove the ` uri ` key from your
2721` database ` configuration and replace it with the ` managed ` key set to ` true ` .
28- Set the ` project ` key in your ` cloud ` configuration to the value of your
29- project ID, obtained via the [ dashboard] ( https://dashboard.sqlc.dev ) .
22+ Access to a running database server is required. Add a connection string to the ` servers ` mapping.
3023
3124``` yaml
3225version : ' 2'
33- cloud :
34- project : ' <PROJECT_ID>'
26+ servers :
27+ - engine : postgresql
28+ uri : " postgres://locahost:5432/postgres?sslmode=disable"
3529sql :
3630- schema : schema.sql
3731 queries : query.sql
4034 managed : true
4135` ` `
4236
43- ### Authentication
37+ An environment variable can also be used via the ` ${}` syntax.
4438
45- ` sqlc` expects to find a valid auth token in the value of the `SQLC_AUTH_TOKEN`
46- environment variable. You can create an auth token via the [dashboard](https://dashboard.sqlc.dev).
47-
48- ` ` ` shell
49- export SQLC_AUTH_TOKEN=sqlc_xxxxxxxx
39+ ` ` ` yaml
40+ version: '2'
41+ servers:
42+ - engine: postgresql
43+ uri: ${DATABASE_URI}
44+ sql:
45+ - schema: schema.sql
46+ queries: query.sql
47+ engine: postgresql
48+ database:
49+ managed: true
5050` ` `
5151
5252# # Improving codegen
@@ -61,8 +61,9 @@ on a per-query basis to speed up future codegen runs. Here's a minimal working c
6161
6262` ` ` yaml
6363version: '2'
64- cloud:
65- project: '<PROJECT_ID>'
64+ servers:
65+ - engine: postgresql
66+ uri: "postgres://locahost:5432/postgres?sslmode=disable"
6667sql:
6768- schema: schema.sql
6869 queries: query.sql
@@ -86,8 +87,9 @@ to ensure the query is valid. Here's a minimal working configuration:
8687
8788` ` ` yaml
8889version: '2'
89- cloud:
90- project: '<PROJECT_ID>'
90+ servers:
91+ - engine: postgresql
92+ uri: "postgres://locahost:5432/postgres?sslmode=disable"
9193sql:
9294- schema: schema.sql
9395 queries: query.sql
9799 rules:
98100 - sqlc/db-prepare
99101` ` `
100-
101- # # With other tools
102-
103- With managed databases configured, `sqlc createdb` will create a hosted ephemeral database with your
104- schema and write the database's connection URI as a string to standard output (stdout). This allows you to use
105- ephemeral databases with other tools that understand database connection strings.
106-
107- In the simplest case, you can use psql to poke around :
108-
109- ` ` ` shell
110- psql $(sqlc createdb)
111- ` ` `
112-
113- Or if you're tired of waiting for us to resolve https://github.com/sqlc-dev/sqlc/issues/296,
114- you can create databases ad hoc to use with pgtyped :
115-
116- ` ` ` shell
117- DATABASE_URL=$(sqlc createdb) npx pgtyped -c config.json
118- ` ` `
119-
120- Here's a minimal working configuration if all you need to use is `sqlc createdb` :
121-
122- ` ` ` yaml
123- version: '2'
124- cloud:
125- project: '<PROJECT_ID>'
126- sql:
127- - schema: schema.sql
128- engine: postgresql
129- database:
130- managed: true
131- ` ` `
0 commit comments