Skip to content

Instantly share code, notes, and snippets.

@zaiste
Last active April 5, 2018 12:02
Show Gist options
  • Save zaiste/5735631 to your computer and use it in GitHub Desktop.
Save zaiste/5735631 to your computer and use it in GitHub Desktop.

Docker PostgreSQL How-To

How to use zaiste/postgresql Docker container.

Docker >= 0.5.0 required.

docker pull zaiste/postgresql
CONTAINER=$(docker run -d zaiste/postgresql)
CONTAINER_IP=$(docker inspect $CONTAINER | grep IPAddress | awk '{ print $2 }' | tr -d ',"')
psql -h $CONTAINER_IP -p 5432 -d docker -U docker -W
password for user docker:
psql (9.2.4)
Type "help" for help.

docker=# create database foo owner=docker;
CREATE DATABASE
@asbjornenge
Copy link

I had to grep for IPAddress when I tried this just now on 0.5.0. Ref my fork. https://gist.github.com/asbjornenge/6078779

@jwarzech
Copy link

jwarzech commented Aug 6, 2013

I am able to log into psql as the 'docker' user with password 'docker' but it appears that this user doesn't have any permissions to create users or databases. Am I supposed to connect to the shell, start postgres, create my users/db, then create new image?

@zaiste
Copy link
Author

zaiste commented Aug 11, 2013

@asbjornenge thanks I've just corrected the gist

@zaiste
Copy link
Author

zaiste commented Aug 12, 2013

@jwarzech I've just simplified the script and made docker superuser with ability to create roles and databases.

@Painted-Fox
Copy link

I've got a recommendation for parsing out the ip address.

I installed the jq command line tool like so...

curl -o jq http://stedolan.github.io/jq/download/linux64/jq
chmod +x jq
sudo mv jq /usr/local/bin

Now, instead of using grep, awk, and tr, you can run the following...

CONTAINER_IP=$(sudo docker inspect $POSTGRES_CONTAINER | jq -r '.[0].NetworkSettings.IPAddress')

The -r switch means give the raw output, otherwise the command will put quotes around the ip address.

@binaryphile
Copy link

Just tried running zaiste/postgresql and it immediately exited status one. Jumping on it with /bin/bash and running which postgres gives nothing found, but which psql does show up.

@sym3tri
Copy link

sym3tri commented Aug 22, 2014

You could also do this to get the IP
$ docker inspect --format='{{.NetworkSettings.IPAddress}}' $INSTANCE_ID

@dragon788
Copy link

Was trying to do the Atlassian Jira in Docker, but getting image invalid for 27cf784147099545 in zaiste/postgresql.

@itainteasy
Copy link

@dragon788

Same error..

[root@localhost atlassian-docker]# docker pull zaiste/postgresql
Trying to pull repository docker.io/zaiste/postgresql ...
0e66fd3d6a6f: Error pulling image (latest) from docker.io/zaiste/postgresql, image ID '27cf784147099545' is invalid 27cf78414709: Error downloading dependent layers failed
Error pulling image (latest) from docker.io/zaiste/postgresql, image ID '27cf784147099545' is invalid

Just grabbing the latest postgresql image does the job
docker search postgresql

Then of course update your docker run command .. we should have used naming adequately.. the wiki is way outdated
docker run -d --name postgres -p=5432:5432 paintedfox/postgresql

The original post that caused some of these questions to be asked is http://blogs.atlassian.com/2013/11/docker-all-the-things-at-atlassian-automation-and-wiring/ ... I'll have to give'em a nudge as well as zaiste's docker image in the repo (https://hub.docker.com/r/zaiste/postgresql/).

Best of luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment