Skip to content

Instantly share code, notes, and snippets.

@themathmagician
Last active September 20, 2018 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themathmagician/ff739764d97be4592318d764b51e0251 to your computer and use it in GitHub Desktop.
Save themathmagician/ff739764d97be4592318d764b51e0251 to your computer and use it in GitHub Desktop.
Postgresql + Docker

Runs a docker container with default postgres image

--name sets name to purposedb,

-e injecting the env variable POSTGRES_PASSWORD

-d run the container in the background

-p maps containerport to localhost port

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

Check that container is running

$docker ps

Enter the running container and open bash

-i interactive

-t pseudo terminal

docker exec -it 13b4318abb53 bash

Open the psql tool on the container

-U user name

-W to prompt password

-p what port to communicate

-h ip adress of host

$ psql -h localhost -p 5432 -U postgres -W

#Log directly into the psql tool on the container $docker exec -it 61566d09e3bd psql -U postgres -W

create database

CREATE DATABASE test;

check that it is there

\l

See users

\du

#Se users as select SELECT * FROM pg_roles;

#See tables (on current connection) \dt

#Connect to database \c db_name

#exit \q

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