Script Bytes

Tutorials and Tips for Angular, .Net, and More

Run Postgres in Docker!

Jeff F
Jeff F
Cover Image for Run Postgres in Docker!

Running your Postgres database inside a docker container on your local computer is a great way to test out changes to the database before you push them up to your dev or test environment. I do this all the time on my laptop when developing and absolutely love it.

Learn how to run Postgres in Docker!

Pull Docker Image

Most of the setup and instructions I'm showing can be found at the docker hub page for Postgres.

The first thing you'll need to do is pull the docker image you want to use. As of this writing the latest version of Postgres is 15.1. Open up a terminal and run the command docker pull postgres. This will pull the latest Postgres docker image onto your computer.

By default the command above will pull the latest image. If you want to pull an older version, for example 14.6, use docker pull postgres:14.6

Run the Docker Image

Next you just need to run the image and you're all set. The command to run the docker image is:

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres

There's a lot here, so let's break this down. docker run is the command we are running. This command runs an image inside of a container. You can read more about it here if you would like: docker run reference.

--name some-postgres is giving our new container a name of some-postgres. You can change this to anything you'd like to make your containers easier to identify.

-e POSTGRES_PASSWORD=mysecretpassword is passing in an environment variable with the name of POSTGRES_PASSWORD, and a value of mysecretpassword. This is the password that you use to connect to the database when it is running.

-d means run it in detached mode. Detached mode means that it won't keep your terminal locked as it's running.

-p 5432:5432 is mapping the ports. The left side is the host port, aka the port on your computer. The right side is the port it maps to inside the container. Since Postgres listens on port 5432 by default, this is what we want to use.

Lastly we pass the name of the image we want to run: postgres. If you were running a different image, for example version 14.6, you would use postgres:14.6

Once the docker run command is run, you should see the container up and running. This means the database is now ready to use.

If you're interested in running a SQL Server database in docker, I wrote an article on that too! Run SQL Server in Docker.

Connecting to the Database

To connect to the database, the server name is localhost, the username is postgres, and the password is the one you pass in the docker run command above, in this demo it is mysecretpassword.

Connection Information Screenshot

Once connected you're all set and your database is ready to use.

You can stop and restart your container without any issues. However if you delete your container, you will lose everything in your database. If you want to make sure you don't lose any data, even when removing your containers, then you need to use a volume to persist your data

Persist Data Using a Volume

In order to prevent losing your database when you remove and restart your container, you'll need to set up a volume when running your container. Luckily this is very easy.

All you have to do is pass in one more parameter to the docker run command. The parameter is: -v /local/folder:/var/lib/postgresql/data. This is telling docker that you want to map an internal directory of the container to a directory on your host machine. The two folders are separated by a :. On the left side is the host machine folder, and the right side is the folder inside the docker container.

In this case, postgres will by default use the /var/lib/postgresql/data folder to store all of the database information. Update the /local/folder path to the desired folder on your machine where you want the data to live.

So the full command is now:

docker run --name postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 -v /local/folder:/var/lib/postgresql/data postgres

If you run this command with your correct folder you'll be all set.

Here is a screenshot of my folder after running the above command. As long as you don't delete this folder from your machine, and use it as your volume path, you'll always have your database in tact.

Local Postgres Data Folder Screenshot