These are my notes for running Postgres in a Docker container for use with a local Django or Rails development server running on the host machine (not in Docker). Running in Docker allows keeping my database environment isolated from the rest of my system and allows running multiple versions and instances. (I previously had a problem where Homebrew upgraded Postgres when I didn't expect it to and my existing database became incompatible. Admittedly, I didn't know Homebrew well, but it was frustrating.) Disadvantages of Docker are it's another layer of abstraction to learn and interact with. We use Docker extensively at work, so from a mental overhead point of view, it's something I wanted to learn anyways. Currently I use the Homebrew Postgres for work, and Postgres in Docker for personal projects. I also wrote some notes on Postgres and Homebrew here.
$ psql -U postgres postgres=# alter user postgres with password 'NEWPASSWORD'; postgresl=# q Reset the PostgreSQL password. If you don’t remember your PostgreSQL database password, you can follow the steps below to reset it to a new value: Change the authentication method in the PostgreSQL configuration file pghba.conf from md5 to trust. In Terminal run the following command to install PostgreSQL on Mac using Homebrew. $ brew install postgres We can check the version of PostgreSQL using the psql command. $ psql -version psql (PostgreSQL) 11.5 Start PostgreSQL. To start PostgreSQL run the following command in the Terminal. $ brew services start postgres We will get a similar.
Install Docker¶
Install Docker for Mac: https://docs.docker.com/docker-for-mac/install/.
Alternatively, you can install Docker using Homebrew: brew install homebrew/cask/docker
OPTION 1: Run Postgres using a single Docker command ¶
Run a postgres container- uses the official docker postgres 13 image
- uses a named volume,
my_dbdata
, to store postgres data - exposes port 54320 to the host using
-p
- sets the container name to
my_postgres
- uses the
-d
flag to run in the background - sets the postgres superuser password to
'my_password'
using-e
and thePOSTGRES_PASSWORD
environment variable.
OPTION 2: Run Postgres using Docker Compose¶
Mac Postgresql Client
Create adocker-compose.yml
fileCreate a new file docker-compose.yml
:
- uses docker compose file version 3
- sets up a service named
'db'
(this name can be used withdocker-compose
commands) - uses the
postgres:13
image from hub.docker.com using theimage
key - creates a container named
'my_postgres'
using thecontainer_name
key - sets the postgres superuser password to
'my_password'
using theenvironment
key and thePOSTGRES_PASSWORD
environment variable - connects port 5432 inside Docker as port 54320 on the host machine using the
ports
key - uses a named volume,
'my_dbdata'
, for storing the database data using thevolumes
key. Even if the container and image are deleted, the volume will remain unless explicitly deleted usingdocker volume rm
. - for more information, see the Docker Compose file reference
Pull the postgres image from hub.docker.com, create a container named 'my_postgres', and start it in the background:
See that it's working¶
See the logs:
Try running psql:
Mac Postgres Password Change
hit CTRL+D to exit
For other commands such as starting, stopping, listing or deleting, see my Docker cheat sheet.
Stop Postgres Mac
Create a database¶
Connect using Python and psycopg2
¶
Create a new file named myscript.py
Run it
Mac Postgres Password Manager
Errors¶
If you get the above error, you can remove the container by running:
If you get the above error, you can stop the container by running: