Skip to content

Setting up a PostgreSQL database for local testing

Vera edited this page Nov 15, 2018 · 10 revisions

Prerequisites

  1. Do not have PostgreSQL already installed. This guide assumes that it will be setting up the postgres container on the default port.
  2. Windows 10 Professional, Enterprise, or Server 2016 with a CPU supporting virtualization features OR Mac OSX 10.11+
  3. When running in Windows, you'll need to run an Administrator command prompt or powershell prompt for the docker commands. You can do this by right-clicking the Windows icon in the taskbar, then selecting 'Command Prompt (Admin)' or `Windows Powershell (Admin)'.

Setting up a local PostgreSQL test environment

  1. Sign up for Docker (making note of your credentials) and download Docker for Desktop: https://www.docker.com/products/docker-desktop
  2. Once docker is installed, run the following command to download the postgres container template.
    • docker pull postgres:10-alpine
  3. Run the following command to create a postgres container, replacing 'mysecretpassword' with an arbitrary password to connect to postgres with.
    • docker run --name anywhere-postgres --publish 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres:10-alpine
      • Breakdown of the command
        • 'docker run': Runs a process in an isolated container.
        • '--name': Designates the container's name.
        • '--publish': Maps an external port to an internal port in the container. Without this, the container will have no external access.
        • '-e': Allows you to set environmental variables published by the container. The environmental variables are created during container template creation.
        • '-d': Daemonizes the container, causing it to run as a background process.
        • 'postgres:10-alpine': Defines the container template to use for the container.
  4. Run the command docker ps -a to list out all the containers on your system. Verify that the 'anywhere-postgres' containers STATUS is listed as up.
  5. Download and install pgAdmin 4 for Windows (exe) or OS X (dmg): https://www.pgadmin.org/download/
  6. Start pgAdmin, right-click on 'Servers' in the left pane, then select 'Create > Server...'
  7. Under the General tab, give the server a memorable name, like 'anywhere-postgres'.
  8. Under the Connection tab, set 'Host name/address' to 'localhost'.
  9. Click 'Save'. You'll be prompted for the 'mysecretpassword' password you set while creating the postgres container with Docker.
  10. You have now verified that your copy of PostgreSQL is running and accepting connections! You can connect to postgres from other apps with the hostname 'localhost', the username 'postgres', the password you set above, and the default database 'postgres'.

Notes

General notes

  • docker ps -a lists of all the containers on your system.
  • docker stop anywhere-postgres will stop the container named 'anywhere-postgres'.
  • docker start anywhere-postgres will start the container named 'anywhere-postgres'.
  • docker image ls will list the container templates you've downloaded. You can use these to create new containers with docker run.

Notes for Windows

  • Docker for Windows install guide: https://docs.docker.com/docker-for-windows/install/
  • On Windows, Docker sets up a Hyper-V virtual machine on install to run docker containers on by default.
  • After Hyper-V is enabled, VirtualBox no longer works, but any VirtualBox VM images remain. The inverse is also true. Installing VirtualBox after Hyper-V will make any Hyper-V/Docker images no longer work.

Notes for OSX

  • Docker for OSX install guide: https://docs.docker.com/docker-for-mac/install/
  • For OSX, VirtualBox prior to version 4.3.30 must NOT be installed (it is incompatible with Docker for Mac). If you have a newer version of VirtualBox installed, it’s fine.