Getting started — Postgres in Docker

  • Setup Docker Image
// Install Docker by following instructions here: https://docs.docker.com/desktop/mac/install/// Download Postgresql Docker image from the docker registry
$ docker pull postgres
// run the container
$ docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
// Enter into interactive shell session
$ docker exec -it some-postgres /bin/bash
  • Creating a postgresql table and index
// role as a postgres user 
root@abab43cfc38d:/# su postgres
// get connection info
postgres=# \conninfo
> You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
// Extensions (to generate uuid)
postgres=# CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
// Create a table
postgres=# CREATE TABLE IF NOT EXISTS example
( id UUID NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY, name VARCHAR(256) NOT NULL)
// Create index on both ID, Name fields
postgres=# CREATE INDEX IF NOT EXISTS idx_exampe_id_name ON example(id, name);

--

--

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