dwyl / learn-postgresql

🐘 Learn how to use PostgreSQL and Structured Query Language (SQL) to store and query your relational data. 🔍
212 stars 23 forks source link

Run Postgres in a Docker container #55

Open SimonLab opened 5 years ago

SimonLab commented 5 years ago

We can add a section on how to run postgres in a Docker container. This is useful when you don't want to setup Postgres on your machine.

1 Get Postgres from docker: docker pull postgres

2 Start a new postgres container: docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=mydb -d -p 5432:5432 postgres

where --rm clean up the container, ie remove the container when it stops, see https://docs.docker.com/engine/reference/run/#clean-up-rm

--name is the name of the container, you can choose the name you want

-e for defining some environment variables, here we are setting the password and the database name

-d to run the container as a deamon, ie as a background process

-p to expose the port of the container, this allow us to connect to postgres with psql for example

3 Check the container has been created with docker container ls

4 Connect to the database with psql psql -h localhost -U postgres -d mydb

5 To stop postgres and the container: docker stop pg-docker where pg-docker is the name of the container

nelsonic commented 5 years ago

@SimonLab any chance you can add this to https://github.com/dwyl/learn-docker ? 💭