Having a working PostgreSQL server is nice, but it quickly becomes onerous to manually create databases and tables. There is a way to bootstrap our server with our schema definition and possibly some seed data which is required for our application to function properly.
To achieve this, we will use our own image, which extends the official one by adding our schema definitions to it.
There are two main differences compared to docker-compose.yml in Lab 1:
- we will be using our own image, so instead of specifying the stock one with
imageoption, we're pointing it to the folder which contains ourDockerfile:
build: ./postgres_server- we are setting the default user and the password in the environment variables:
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password1Take note of the password, it will be required to connect to the server from now on.
Take a look at the Dockerfile that we will be using for our server. It's very simple, all it's doing is copying docker-entrypoint-initdb.d/ folder to the image.
COPY ./docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/When a container is created from this image, all files in this folder with *.sh or *.sql extension will get executed in alphabetical order.
01-init_db.shis a shell script which uses psql to pass commands to PostgreSQL. Notice that we perform some basic hardening, so that our new user cannot connect to the systempostgresdatabase.02-create_table.sqlis a SQL file with some embedded psql metacommands. In this case it's\c users, which connects to theusersdatabase — this is similar toUSE users;in SQL server. This file also demonstrates some of the features specific to PostgreSQL, such as table inheritance, time ranges, and specialised data types.03-insert_dummydata.sqladds some dummy data to the table.
docker-compose up -d
Now try to connect to the system database using our new user:
docker-compose exec postgres_server psql -U docker -d postgres
We revoked public access from postgres database, so you should see an error:
psql: FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
This error will also show up in the container logs:
docker-compose logs -f postgres_server
postgres_server_1 | FATAL: permission denied for database "postgres"
postgres_server_1 | DETAIL: User does not have CONNECT privilege.
docker role can connect to the audit database:
docker-compose exec postgres_server psql -U docker -d audit
After we're in, we can start looking around, but for now let's use GUI tools, as psql will be covered in the next lab.
Connect to the server again, remembering to specify the password.
You can find our new table buried deep inside the audit database.
In DataGrip you apparently connect to a database, not to the server as a whole. Edit the connection settings by specifying the database (audit) and the password (password1), then connect to the database and click More schemas.

Clean up before continuing to the next lab:
docker-compose down
