Skip to content

Commit 77dae84

Browse files
victorpacynabandle
andauthored
rework preloading data section (#145)
* rework preloading data section * rework user creation section * remove .zst as supported files Co-authored-by: Max <max@cedardb.com> --------- Co-authored-by: Max <max@cedardb.com>
1 parent 6267913 commit 77dae84

1 file changed

Lines changed: 57 additions & 40 deletions

File tree

content/get_started/install_with_docker.md

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Connect using a PostgreSQL client:
3030
PGPASSWORD=test psql -h localhost -U postgres
3131

3232
postgres= SELECT 1 as foo;
33-
foo
33+
foo
3434
-----
3535
1
3636
(1 row)
@@ -49,6 +49,7 @@ To retain your database across container restarts, mount a host directory to sto
4949
mkdir -p /opt/cedardb
5050
docker run --rm -p 5432:5432 -v /opt/cedardb:/var/lib/cedardb/data -e CEDAR_PASSWORD=test cedardb/cedardb
5151
```
52+
5253
This stores the database in `/opt/cedardb` on your host.
5354

5455
{{< callout type="info" >}}
@@ -61,79 +62,101 @@ In this case, CedarDB ignores the `CEDAR_USER` and `CEDAR_PASSWORD` environment
6162
the existing credentials to be used.
6263
{{< /callout >}}
6364

64-
6565
{{< callout type="info" >}}
6666
Ensure the mounted database directory resides on a reasonably fast SSD for best performance.
6767
{{< /callout >}}
6868

6969
### Custom credentials
70+
7071
You can configure the initial user and database using environment variables, domain sockets, or secrets.
7172

7273
#### Environment variables
74+
7375
```shell
7476
docker run --rm -p 5432:5432 \
7577
-e CEDAR_USER=test \
7678
-e CEDAR_PASSWORD=test \
7779
-e CEDAR_DB=db \
7880
cedardb/cedardb
7981
```
82+
8083
This command creates a superuser `test` with password `test` and a a database named `db`.
8184

8285
Connect like this:
86+
8387
```shell
8488
psql -h localhost -U test -d db
8589
```
8690

8791
The parameters `CEDAR_USER` and `CEDAR_DB` are optional:
92+
8893
- If `CEDAR_USER` is not set, it defaults to `postgres`.
8994
- If `CEDAR_DB` is not set, it defaults to the value of `CEDAR_USER`.
9095

96+
#### Docker secrets
9197

92-
#### Domain socket authentication
98+
CedarDB can also read credentials from files, ideal for secret management:
9399

94-
To avoid passing credentials in the shell (e.g., because you don't want it to appear in your history), you can run CedarDB in domain socket-only mode:
95100
```shell
96101
docker run --rm -p 5432:5432 \
97-
-e CEDAR_DOMAIN_SOCKET_ONLY=yes \
98-
--name cedardb \
99-
cedardb/cedardb
102+
-e CEDAR_USER_FILE=/run/secrets/user_file \
103+
-e CEDAR_PASSWORD_FILE=/run/secrets/password_file \
104+
-e CEDAR_DB=/run/secrets/db_file cedardb/cedardb
100105
```
101106

102-
Then connect from the same host using the domain socket:
103-
```shell
104-
docker exec -it cedardb psql -U postgres
105-
```
106-
Once connected, you can [manually create users and databases](/docs/references/sqlreference/statements/createrole):
107-
```sql
108-
create user {{username}} superuser with password '1234';
109-
create database {{username}};
107+
You can manage such files using [Docker secrets](https://docs.docker.com/engine/swarm/secrets/).
108+
109+
### Initialization scripts
110+
111+
CedarDB supports auto-initializing a new database with SQL and shell scripts. Additionally, the docker image accepts `xz` or `gzip` compressed SQL files.
112+
Files in `/docker-entrypoint-initdb.d/` are executed or sourced during container setup. Supported file extensions are `.sh`, `.sql`, `.sql.xz` and `sql.gz`.
113+
114+
`.sh` files can use the `process_sql` function to run modified SQL statements that need shell pre-processing, e.g. by expanding shell or env variables.
115+
116+
#### Example: Creating an additional user at DB initialization
117+
118+
Initialization files let you create additional [users and databases](/docs/references/sqlreference/statements/createrole) during first-time setup. Provide usernames and passwords via environment variables or Docker secrets.
119+
120+
```shell {filename="users/create-user.sh"}
121+
#!/bin/bash
122+
set -e
123+
124+
process_sql <<-EOSQL
125+
create role ${NEW_USER} login with password '${NEW_USER_PWD}';
126+
create database ${NEW_USER};
127+
EOSQL
110128
```
111129

112-
#### Docker secrets
130+
Then run:
113131

114-
CedarDB can also read credentials from files, ideal for secret management:
115132
```shell
116-
docker run --rm -p 5432:5432 \
117-
-e CEDAR_USER_FILE=/run/secrets/user_file \
118-
-e CEDAR_PASSWORD_FILE=/run/secrets/password_file \
119-
-e CEDAR_DB=/run/secrets/db_file cedardb/cedardb
133+
docker run -v ./users/:/docker-entrypoint-initdb.d/ \
134+
-p 5432:5432 -e CEDAR_PASSWORD=test \
135+
-e NEW_USER=nonroot -e NEW_USER_PWD=1234 \
136+
--rm cedardb/cedardb
120137
```
121-
You can manage such files using [Docker secrets](https://docs.docker.com/engine/swarm/secrets/).
122138

123-
### Preloading data
124-
CedarDB supports auto-initializing a new database with SQL and shell scripts.
125-
Any file in `/docker-entrypoint-initdb.d/` is executed or sourced during container setup.
139+
Connect with the new user:
140+
141+
```shell
142+
# Connect to CedarDB
143+
psql -h localhost -U nonroot -d nonroot
144+
# Enter '1234' as password
145+
```
126146

127-
#### Example: Build a custom image
147+
#### Example: Preloading data with .sh and .sql files
128148

129149
Create a `movies/` directory with the following files:
130150

131151
```shell {filename="movies/foo.sh"}
132152
#!/bin/bash
133153
set -e
134-
psql -v ON_ERROR_STOP=1 --username "$CEDAR_USER" --dbname "$CEDAR_DB" <<-EOSQL
135-
CREATE TABLE foo(a int);
136-
INSERT INTO foo VALUES (7);
154+
TABLE_NAME=foo
155+
VALUE=7
156+
157+
process_sql <<-EOSQL
158+
CREATE TABLE ${TABLE_NAME}(a int);
159+
INSERT INTO ${TABLE_NAME} VALUES (${VALUE});
137160
EOSQL
138161
```
139162

@@ -152,19 +175,16 @@ insert into movies values
152175
(3, 'Das Boot', 1981, 149, 'Drama');
153176
```
154177

155-
```Dockerfile {filename="movies/Dockerfile"}
156-
FROM cedardb/cedardb
157-
COPY movies.sql foo.sh /docker-entrypoint-initdb.d/
158-
```
159-
160-
Then build and run:
178+
Then run:
161179

162180
```shell
163-
docker build -t cedardb-movies movies/
164-
docker run --rm -p 5432:5432 -e CEDAR_PASSWORD=test cedardb-movies
181+
docker run -v ./movies/:/docker-entrypoint-initdb.d/ \
182+
-p 5432:5432 -e CEDAR_PASSWORD=test \
183+
--rm cedardb/cedardb
165184
```
166185

167186
Connect and inspect the data:
187+
168188
```shell
169189
# Connect to CedarDB
170190
psql -h localhost -U postgres
@@ -195,9 +215,6 @@ postgres= select * from foo;
195215

196216
```
197217
198-
In addition to plain shell scripts and sql files, the CedarDB docker image also accepts `xz`, `gzip`, or `zstd` compressed sql files.
199-
A file must have one of the following extensions: `.sql`, `.sql.gz`, `sql.xz`, `sql.zst`, or `.sh`.
200-
201218
{{< callout type="info" >}}
202219
If you have obtained an enterprise license, refer to the [licensing page](../../licensing) for a step-by-step guide on how to activate it.
203220
{{< /callout >}}

0 commit comments

Comments
 (0)