Skip to content

PostgreSQL Database

Setting Up PostgreSQL Locally

PostgreSQL is a powerful, open-source object-relational database system known for its robustness and advanced features. This guide provides instructions for installing and configuring PostgreSQL both natively and using Docker. Installing PostgreSQL

Terminal window
sudo apt install postgresql

To verify the status of the PostgreSQL service, use:

Terminal window
sudo systemctl status postgresql

Configuring PostgreSQL

  1. Access the PostgreSQL command line interface:
Terminal window
sudo -u postgres psql
  1. Create a new user:
Terminal window
CREATE USER your_user;
  1. Create a new database:
Terminal window
CREATE DATABASE your_db_name;
  1. Set a password for the user:
Terminal window
CREATE USER your_user WITH ENCRYPTED PASSWORD 'your_password';
  1. Grant all privileges on the database to the user:
Terminal window
GRANT ALL PRIVILEGES ON DATABASE your_db_name TO your_user;

Installing PostgreSQL Using Docker

  1. Pull the PostgreSQL Docker image:
Terminal window
docker pull postgres
  1. Create a Docker volume to persist PostgreSQL data:
Terminal window
docker volume create CREDEBL
  1. Run the PostgreSQL Docker container:
Terminal window
docker run --name CREDEBL -e POSTGRES_PASSWORD=credebl_password -d -p 5432:5432 -v CREDEBL:/var/lib/postgresql/data postgres
  1. Verify that the Docker container is running:
Terminal window
docker ps

You should see the container named CREDEBL listed.