Back to all posts

Run PostgreSQL in Docker with NVDA

Inspect the PostgreSQL service, connect with psql, create a practice database and verify the result using VS Code and NVDA.

Watch the video

This lesson adds a PostgreSQL database to the tic tac toe development project. I inspect the Docker Compose service, connect to PostgreSQL, create a database called tictactoe, and read the result with NVDA. Creating a database is the outcome here; creating game tables and connecting the interface come later.

Start from the game project

Have Docker Desktop installed and follow the run the game walkthrough. Open the repository in VS Code, with your terminal at its root beside compose.yaml. I use WSL Ubuntu in VS Code for this demonstration.

The repository has evolved since the August lesson. Today's download includes the later Django API as well as the game and database. The commands below use the database service name shared by the recorded and current versions. Do not replace the whole current Compose file with an older example.

Read the database service

In compose.yaml, find the service named database. The recording uses the postgres:17 image, restart: unless-stopped, the POSTGRES_PASSWORD environment variable, and a named postgres_data volume mounted at /var/lib/postgresql/data. That volume keeps database files outside the container's writable layer.

The sample password is for local development. The current project does not publish PostgreSQL's port to the host. Follow the project README for the current configuration. This lesson is not a production database setup.

Start and inspect PostgreSQL

Run these from the project root:

docker compose up -d
docker compose ps
docker compose exec database pg_isready -U postgres

The readiness command is an additional check from the current README. The video uses docker ps to inspect running containers. In VS Code, Ctrl+grave opens the integrated terminal. Alt+F2 opens Accessible View so you can read long output a line at a time; Escape returns to the terminal.

Connect with psql

The word database below is the Compose service name. It is not the name of the database we are about to create. The uppercase U selects the PostgreSQL user:

docker compose exec database psql -U postgres

You should reach a PostgreSQL prompt. From there, use the psql command below to list databases. It is a backslash followed by lowercase L, not a slash or the number one:

\l

Create and verify the practice database

If tictactoe is not already listed, enter this SQL statement, including its semicolon:

CREATE DATABASE tictactoe;

A successful statement reports CREATE DATABASE. Run the list command again and find tictactoe in the output:

\l

If it already exists, use the existing practice database rather than deleting it. The demonstration creates a database, not a table. See the PostgreSQL psql reference for listing and session commands.

Leave psql and stop the project

I use Ctrl+D to leave psql in the WSL terminal. You can also enter its quit command:

\q

Back at the shell prompt, stop the project and check its containers:

docker compose down
docker compose ps

Normal down preserves the named volume. Do not add --volumes when you intend to keep your database; Docker documents that option as volume removal. In the Django API lesson, I build a service that connects to PostgreSQL and checks its health.