Installing PostgreSQL in a Docker container on your local machine
Suggest editsPrerequisites
- Docker-compatible OS (macOS, Windows, Linux)
Using Docker for your local PostgreSQL development environment streamlines setup, ensures consistency, and simplifies management. It provides a flexible, isolated, and portable solution that can adapt to various development needs and workflows.
Preparing Docker
Install Docker:
Make sure Docker is installed on your machine or download and install it from Docker’s official website.
- macOS: Download and install Docker Desktop from Docker’s official website.
- Windows: Download and install Docker Desktop from Docker’s official website. Ensure WSL 2 is enabled if using Windows 10 or later.
- Linux: Install Docker using your distribution’s package manager. For example, on Ubuntu:
Pull the PostgreSQL Docker image:
Open a terminal or command prompt and run the following command to pull the latest PostgreSQL image from Docker Hub:
Running the PostgreSQL container
Run a new container with the PostgreSQL image using the following command:
--name my_postgres
The --name
flag tells docker to creates a new container named my_postgres
.
-d
The -d
flag tells Docker to run the container in detached mode. This means the container runs in the background and does not block the terminal.
postgres
This is the name of the image to run. Docker uses this name to pull the image from Docker Hub if it is not already present on the local machine. Note that if we had not pulled it, this command would automatically pull the PostgreSQL image.
-e POSTGRES_PASSWORD=mysecretpassword
The -e
flag sets an environment variable POSTGRES_PASSWORD
to mysecretpassword
. This is used the password for the default postgres
user. You should use a different password.
-v my_pgdata:/var/lib/postgresql/data
Docker uses volumes to persist data in Docker containers. This flag mounts a volume named my_pgdata
to persist data.
The data in this case is whatever Postgres writes to the /var/lib/postgresql/data
directory within the container.
These writes are persisted outside the container in a docker volume; the command docker volume inspect my_pgdata
will show you information about that volume.
-p 5432:5432
The -p
flag maps the container’s port 5432 to the host machine’s port 5432. Port 5432 is Postgres's default port for communications. By using this flag, it allows you to access the PostgreSQL database from your host machine.
Verifying the container is running
To verify that the container is running, use the following command:
This command lists all running containers. You should see the my_postgres
container listed.
You now have a persistent, locally accessible Postgres database running in a Docker container. You can now start using it.
Access PostgreSQL with a client
To access the PostgreSQL database, without any additional tools, you can use the following command to open a PostgreSQL prompt:
This logs into the Docker container and runs the psql
command as the postgres
user from there.
The psql
command is a powerful tool for interacting with PostgreSQL databases. You should install it on your local machine to interact with the PostgreSQL database running in the Docker container.
macOS
You can install the PostgreSQL client using Homebrew:
Windows
Download the PostgreSQL client from the official website.
Linux
Use your distribution’s package manager to install the PostgreSQL client. For example, on Ubuntu:
Connecting other apps
You can also connect other applications to the PostgreSQL database running in the Docker container. You need to provide the following connection details:
- Host:
localhost
- Port:
5432
- Username:
postgres
- Password: (whatever you set it to)
- Database:
postgres
Or use the connection string:
Verifying data persistence
Create a table and insert data.
Access the PostgreSQL instance and run the following SQL commands to create a table with columns and insert some data:Stop and completely remove the container.
Recreate the container with the same volume.