If you and your team are working on a containerized project, you often need a database to store information. PostgreSQL is one of the most popular database systems available today. In this guide, we’ll walk you through how to install PostgreSQL in your Docker container on Windows.
If you don’t already have Docker, you can download the installer from the Docker website. Run the installer with the default configuration options.
After installing, you will need to restart your machine.
Check out the list of Postgres versions available in docker. To get the latest one, use pull
:
docker pull postgres
The PostgreSQL image requires that you supply a root password. You cannot set this in the Docker Desktop GUI and must do so through the command line:
docker run --name my-postgres -p 5432:5432 -e POSTGRES_PASSWORD=mypassword -d postgres
The -p
flag in this command forwards the PostgreSQL server port from Docker to the host machine. In this case, we are not changing the port, so both values will be the same.
If you don’t already have a database and user, you can follow these steps to create them now. First, navigate to your PostgreSQL container in Docker and click the ‘CLI’ button to launch a shell client:
A terminal window should appear. First run psql -d postgres -U postgres
and enter your specified root password when prompted. Once you reach the PostgreSQL prompt, run the following commands, substituting myDB, myUser and myPassword for your own desired information.
CREATE DATABASE myDB;
CREATE USER myUser WITH PASSWORD ‘myPassword’;
GRANT ALL PRIVILEGES ON myDB TO myUser;
To connect to your new Postgres database server from an SQL client, you’ll need the following information:
IP/Host: If your docker container is deployed on a remote machine or cluster, you run this command to get the IP address:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' YOUR_CONTAINER_NAME
Otherwise, if your docker container is deployed on your local machine, your hostname will simply be localhost
.
Port: Docker will forward the port of your PostgreSQL server to any port specified in the run command (above). This should be 5432 unless otherwise specified.
Database: The name of the schema containing your data within the Postgres instance. From our example above, this would be myDB
.
User: The username of a user granted privileges on the specified database schema. From our example above, this would be myUser
.
Password: The password for your user, from our example above, this would be myPassword
.
In the Arctype connection screen, enter the above information and click Test Connection
. If everything is entered correctly, you should see a ‘Successfully connected to PostgreSQL’ message appear at the bottom of the screen:
After confirming your connection details, press Save!