You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The SQL script creates a new database called `sampledb`, connects to it, and creates a `users` table. The table includes an auto-incrementing `id` as the primary key, a `name` field with a maximum length of 50 characters, and a unique `email` field with up to 100 characters.
@@ -151,162 +151,76 @@ Now that you have learned how to launch Postgres and pre-seed the database using
151
151
152
152
Make sure you stop any running Postgres containers (along with volumes) to prevent port conflicts before you follow the steps:
153
153
154
-
1.Create a named volume.
154
+
1.Modify the `seed.sql` with the following entries:
155
155
156
-
Use the `docker volume create` command to create a named volume.
This command mounts your `seed.sql` file from the current directory (`$(pwd)/seed.sql`) into the temporary container at `/sql-files`, and then copies it into the `data_sql` named volume.
187
-
188
-
> [!TIP] Running on Windows
189
-
> When running this command on Windows, use `${PWD}` (in uppercase and with curly brackets) instead of `$(pwd)`, and make sure to execute the command in PowerShell:
178
+
This Dockerfile copies the `seed.sql` script directly into the PostgreSQL container's initialization directory.
This ensures that the volume is mounted correctly on the Windows systems.
200
-
201
-
5. Now that your `seed.sql` file is in the `data_sql` volume, you can run your `mynewpostgres` image and mount the named volume:
202
-
203
-
```console
204
-
$ docker run --name mynewpostgres \
205
-
-p 5432:5432 \
206
-
-e POSTGRES_PASSWORD=mysecretpassword \
207
-
-v data_sql:/docker-entrypoint-initdb.d \
208
-
mynewpostgres
209
-
```
210
-
211
-
Open a new terminal and run the following command to verify the database and tables seeded into the database.
212
-
```console
213
-
psql -h localhost -U postgres sampledb
214
-
```
215
-
216
-
Enter `mysecretpassword` when prompted for the password. Run the following command to verify if the table named users is populated in the database `sampledb` or not.
Now that you’ve been shown how to pre-seed a database by using volumes, let’s see how you can simplify the whole process of seeding by using a single Docker Compose file.
229
-
230
-
> [!TIP]
231
-
> Make sure you stop any running Postgres containers(along with volumes) to prevent port conflicts before you follow the next steps.
232
-
233
-
First, you will need to create the following project directory structure:
234
-
235
-
```console
236
-
$ tree
237
-
.
238
-
├── compose.yml
239
-
└── sql_files
240
-
└── seed.sql
241
-
```
242
180
243
-
1. Start by writing the compose file
244
-
245
-
This compose.yml file defines a Postgres service named `db` using the latest Postgres image, which sets up a database with the name `sampledb`, along with a user `postgres` and a password `mysecretpassword`.
181
+
3. Use Docker Compose.
182
+
183
+
Using Docker Compose makes it even easier to manage and deploy the PostgreSQL container with the seeded database. This compose.yml file defines a Postgres service named `db` using the latest Postgres image, which sets up a database with the name `sampledb`, along with a user `postgres` and a password `mysecretpassword`.
246
184
247
185
```yaml
248
186
services:
249
-
db:
250
-
image: postgres:latest
251
-
container_name: my_postgres_db
252
-
environment:
253
-
POSTGRES_USER: postgres
254
-
POSTGRES_PASSWORD: mysecretpassword
255
-
POSTGRES_DB: sampledb
187
+
db:
188
+
build:
189
+
context: .
190
+
dockerfile: Dockerfile
191
+
container_name: my_postgres_db
192
+
environment:
193
+
POSTGRES_USER: postgres
194
+
POSTGRES_PASSWORD: mysecretpassword
195
+
POSTGRES_DB: sampledb
256
196
ports:
257
197
- "5432:5432"
258
198
volumes:
259
199
- data_sql:/var/lib/postgresql/data # Persistent data storage
260
-
- ./sql_files:/docker-entrypoint-initdb.d # Mount local sql file to seed the database
261
200
262
-
volumes:
263
-
data_sql:
201
+
volumes:
202
+
data_sql:
264
203
```
265
204
266
-
It maps port `5432` on the host to the container's `5432`, let you access to the Postgres database from outside the container. It also defines two volumes: one (`data_sql`) for persisting the database data, ensuring that data is not lost when the container is stopped, and another volume that mounts the local `sql_files` directory into `/docker-entrypoint-initdb.d` within the container. This mounted directory contains a SQL file that is automatically executed when the Postgres container is initialized, allowing pre-seeding of the database.
205
+
It maps port `5432` on the host to the container's `5432`, let you access to the Postgres database from outside the container. It also define `data_sql` for persisting the database data, ensuring that data is not lost when the container is stopped.
267
206
268
-
2. Create a new directory `sql_files/` and copy the following `seed.sql` to this new directory:
269
207
270
-
```plaintext
271
-
-- Ensure the users table is created in the sampledb database
This SQL script ensures that the users table is created in the `sampledb` database if it doesn't already exist. The table includes three columns: id, which is a serial primary key, name (a VARCHAR of 50 characters), and email (a VARCHAR of 100 characters that must be unique). The script also inserts three user records into the users table, but if a conflict occurs on the email field (i.e., if the email already exists), the insertion is skipped, ensuring no duplicate emails are added.
288
208
289
209
3. Bring up the Compose service.
290
210
211
+
Assuming that you've placed the `seed.sql` file in the same directory as the Dockerfile, execute the following command:
212
+
291
213
```console
292
-
$ docker compose up -d
214
+
$ docker compose up -d --build
293
215
```
294
216
295
217
4. It’s time to verify if the table `users` get populated with the data.
Enter `mysecretpassword` when prompted for the password.
302
-
303
-
```plaintext
304
-
Password for user postgres:
305
-
psql (15.8 (Homebrew), server 16.4 (Debian 16.4-1.pgdg120+1))
306
-
WARNING: psql major version 15, server major version 16.
307
-
Some psql features might not work.
308
-
Type "help" for help.
309
-
223
+
```
310
224
sampledb=# select * from users;
311
225
id | name | email
312
226
----+-------+-------------------
@@ -318,15 +232,6 @@ $ tree
318
232
sampledb=#
319
233
```
320
234
321
-
> [!TIP]
322
-
> If you're encountering the error 'more' is not recognized as an internal or external command when running queries in `psql` on a Windows system, this is likely due to an issue with the pager setting, which uses external programs like more or less to paginate query results. You can resolve this by turning off pagination within `psql` using the following command:
323
-
324
-
```console
325
-
\pset pager off
326
-
```
327
-
328
-
To avoid running this command every time, you can permanently disable the pager by adding \pset pager off to your psqlrc.conf file, which is located in the `%APPDATA%\postgresql\` directory on Windows. This will ensure that query results are always displayed without invoking an external pager program.
0 commit comments