A local PostgreSQL development environment based on Docker

  1. Create a directory data in any directory you want.
  2. Create a Docker network using the following command.
docker network create learning-postgreSQL-network 
docker run --rm --name postgres-server -v $(pwd)/data:/var/lib/postgresql/data --network learning-postgreSQL-network -e POSTGRES_PASSWORD=your-password postgres
docker run --rm -it --network learning-postgreSQL-network postgres psql -U postgres -h postgres-server -W
docker run --rm --name postgres-server -v $(pwd)/data:/var/lib/postgresql/data --network learning-postgreSQL-network postgres
  1. A container running a PostgreSQL server.
  2. A client to connect to the PostgreSQL server. The client I want to use is psql. psql is also run in a container.
  3. Data has to be stored locally of course. Any change must be kept in local file system.
docker run --rm --name postgres-server -e POSTGRES_PASSWORD=your-password postgres
docker network create learning-postgreSQL-network
docker run --rm --name postgres-server --network learning-postgreSQL-network -e POSTGRES_PASSWORD=your-password postgres
  • -U designates the user name
  • -h specifies the hostname
  • -W force psql to prompt for a password before connecting to a database
docker run --rm -it --network learning-postgreSQL-network postgres psql -U postgres -h postgres-server -W

One last thing

Everything I do to the database must be kept. However until now everything written to the database will vanish into the air, if the container is stopped and then automatically removed.

docker run --rm --name postgres-server -v $(pwd)/data:/var/lib/postgresql/data --network learning-postgreSQL-network -e POSTGRES_PASSWORD=your-password postgres

Another little thing

After the above command is run, the directory data is populated with necessary data including the password. Next time the password is no longer needed to run a PostgreSQL container. The environment variable POSTGRES_PASSWORD can be ignored. Here is the command after the directory data is populated:

docker run --rm --name postgres-server -v $(pwd)/data:/var/lib/postgresql/data --network learning-postgreSQL-network postgres

Summary

Here I explain the way to run PostgreSQL like a standalone program. The way to run psql as a standalone program is also provided. Every change made to the database cab be locally stored as well.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store