About • Disclaimer • Getting Started • Download • How To Use
PostgreSQL, also known as Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance.
Important
Localhost Databases is not affiliated with the databases' developers/owners and is not an official product.
Localhost Databases has been developed to run databases in a local Docker environment. To install a production instance, read the databases' respective installation guides.
You will need to make sure your system meets the following prerequisites:
- Docker Engine >= 20.10.0
This repository utilizes Docker to run the PostgreSQL sample. So, before using the PostgreSQL, make sure you have Docker installed on your system.
To use PostgreSQL, you can clone the latest version of Localhost Databases repository for macOS, Linux and Windows.
# Clone this repository.
$ git clone [email protected]:luisaveiro/localhost-databases.git --branch main --single-branch
You can locate the PostgreSQL Docker configuration in the databases
directory.
# Navigate to the PostgreSQL folder.
$ cd localhost-databases/databases/pgsql
There are a few steps you need to follow before you can have an PostgreSQL database set up and running in Docker container. I have outline the steps you would need to take to get started.
Before you start a database in a Docker container, you will need to create a DotEnv file. The DotEnv file will allow you to configure your database's credentials and map a container's port.
Localhost Databases includes a .env.example
file for PostgreSQL Database. You
can run the following command in the terminal to create your DotEnv file.
# Navigate to a database.
$ cd databases/pgsql
# Create .env from .env.example.
$ cp .env.example .env
The PostgreSQL Docker Compose file uses the follow variables from the DotEnv file.
#--------------------------------------------------------------------------
# Docker env
#--------------------------------------------------------------------------
# The project name. | default: pgsql
APP_NAME="pgsql"
#--------------------------------------------------------------------------
# Database (PostgreSQL) env
#--------------------------------------------------------------------------
# The PostgreSQL database container name. | default: pgsql_db
DB_CONTAINER_NAME="${APP_NAME}_db"
# The PostgreSQL database configuration. | default: local
DB_DATABASE="local"
# The PostgreSQL database user credentials.
DB_USERNAME=""
DB_PASSWORD=""
#--------------------------------------------------------------------------
# Network env
#--------------------------------------------------------------------------
# Map the database container exposed port to the host port. | default: 5432
DB_PORT=5432
# The Docker network for the containers. | default: local_dbs_network
NETWORK_NAME="local_dbs_network"
#--------------------------------------------------------------------------
# Volume env
#--------------------------------------------------------------------------
# The database container data volume. | default: pgsql_db_data
DB_VOLUME_DATA_NAME="${DB_CONTAINER_NAME}_data"
For a list of available environment variables that the PostgreSQL Docker image supports, you can visit PostgreSQL Docker Hub page.
To start the PostgreSQL container, you can run the following command:
# Navigate to PostgreSQL database.
$ cd databases/pgsql
# Run Docker Compose command.
$ docker compose up -d
To check the PostgreSQL container is running and the port mapping is configured correctly, you can run the following command:
# List containers
$ docker ps
You should see a similar output.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f4213061f5c postgres:latest "docker-entrypoint.s…" 43 seconds ago Up 42 seconds (healthy) 0.0.0.0:5432->5432/tcp pgsql_db
To stop the PostgreSQL container, you can run the following command:
$ docker compose down
To connect to your PostgreSQL container from your database client, you will need to provide the following settings:
HOST=127.0.0.1
PORT="${DB_PORT}"
USER="${DB_USERNAME}"
PASSWORD="${DB_PASSWORD}"
DATABASE="${DB_DATABASE}"
Below is a screenshot of the settings used in TablePlus:
TablePlus settings for PostgreSQL.