Skip to content
JackSparrow414
Go back

PostgreSQL (Part 1): Setting Up a Learning Environment

Table of contents

Open Table of contents

Why Use Docker?

When learning a technology, the most frustrating part at the beginning used to be installing software, setting environment variables, and so on. One incorrect step—or even correct steps followed by strange problems—would send us searching for answers, trying them, failing, and repeating the cycle. By the time the environment finally worked, our enthusiasm for learning was long gone.

That is why I strongly recommend Docker for learning different technologies. Pull the image, and the environment is ready to use immediately. When you finish, delete the container and clean it all up. Very convenient!

Write the docker-compose.yml File

Official PostgreSQL Docker Image

version: "3.7"
services:
  pgdb:
    # Set the container memory size limit
    shm_size: "1gb"
    image: postgres:latest
    # Set the container name
    container_name: pgdb
    # https://docs.docker.com/compose/compose-file/#restart
    restart: always
    # Map the port
    ports:
      - 5432:5432
    # Set the PostgreSQL server time zone; the default is GMT. Official documentation: https://www.postgresql.org/docs/14/app-postgres.html#id-1.9.5.14.6.3
    command: postgres -c timezone='US/Eastern'
    # https://docs.docker.com/compose/compose-file/#long-syntax-4
    volumes:
      - type: volume
        source: pgdb-data
        # All PostgreSQL configuration files are under /var/lib/postgresql/data
        target: /var/lib/postgresql
        read_only: false
    # Location of the environment variable file  https://docs.docker.com/compose/compose-file/#env_file
    env_file:
      - .env
# Declare the volume
volumes:
  pgdb-data:

For more options and explanations, see the Compose file documentation

Create the .env File

vim .env

Use the following contents:

POSTGRES_PASSWORD=12345
POSTGRES_USER=postgres
POSTGRES_DB=postgres
PGDATA=/var/lib/postgresql/data

For additional variables, see the official PostgreSQL image documentation

Start the Container

docker-compose up

Docker Compose logs showing PostgreSQL volume and container creation and database startup The output shows the volume being created, the container being created, and a successful startup.

Install pgAdmin Locally

Download the version for your operating system directly from the pgAdmin website.

On macOS, you can also install it directly with Homebrew.

brew search pgadmin
brew install pgadmin4

Connect to PostgreSQL in Docker with pgAdmin

These settings are defined in the .env file above.

Create a Table

Enter the Container

docker exec -it pgdb bash

Connect to the PostgreSQL Server

psql -U postgres

Select the Database

postgres

Create a Table

CREATE TABLE my_first_table (first_colume text);

List Tables

\dt

Insert Data

INSERT INTO my_first_table (first_colume) VALUES ('hello PosgreSQL');

You can also view this data in pgAdmin. Viewing PostgreSQL databases, tables, and query results in pgAdmin

The PostgreSQL learning environment is now ready.

Other Resources


Share this post:

Continue this series

PostgreSQL

  1. PostgreSQL (Part 1): Setting Up a Learning EnvironmentYou are here
  2. PostgreSQL (Part 2): Best Practices for Procedural Language Usage

Comments

Questions, corrections, and experiences are welcome. Sign in with GitHub to comment; both language versions share this discussion.

Comments are available on the live site only.