Build Your Own Cloud Database in Minutes

Build Your Own Cloud Database in Minutes

# postgres# learning
Build Your Own Cloud Database in Minutesa gorde

This is a step-by-step guide for making a private cloud database for your projects for start you just...

This is a step-by-step guide for making a private cloud database for your projects for start you just need a cloud server

usecase:

  • perfect for personal project, centralized database
  • full control

A. Install PostgreSQL on VM

Im using OVH Cloud as a VM with ubuntu.

# Update the package list to get info on the newest versions of packages
$ sudo apt update

# Install PostgreSQL database server and additional utilities/extensions
$ sudo apt install -y postgresql postgresql-contrib

# Start the PostgreSQL service immediately
$ sudo systemctl start postgresql

# Enable PostgreSQL to start automatically on every system boot
$ sudo systemctl enable postgresql
Enter fullscreen mode Exit fullscreen mode

B. Create a database and user

1- Start PostgreSQL 17

# Start the PostgreSQL cluster (version 17, cluster name "main")
$ sudo pg_ctlcluster 17 main start

# List all PostgreSQL clusters with their version, name, port, status, and data directory
$ sudo pg_lsclusters
Enter fullscreen mode Exit fullscreen mode

Ref Screenshot

2- Create DB and user

# Creating database and setting user
$ sudo -u postgres psql
CREATE DATABASE twohype;
CREATE USER twohype_user WITH PASSWORD 'yourpassword';
GRANT ALL PRIVILEGES ON DATABASE twohype TO twohype_user;
\q
Enter fullscreen mode Exit fullscreen mode

3.1- Allow remote connections

# Open the main PostgreSQL 17 configuration file in the nano text editor
# Find and enable; listen_addresses = '*'

$ sudo nano /etc/postgresql/17/main/postgresql.conf
Enter fullscreen mode Exit fullscreen mode

3.2- Allow remote connections

# Open the host-based configuration file
$ sudo nano /etc/postgresql/17/main/pg_hba.conf

# Add at the bottom:
host    all             all             0.0.0.0/0               md5
Enter fullscreen mode Exit fullscreen mode

Ref Screenshot

4- Restart and open firewall

# Restart the PostgreSQL service
$ sudo systemctl restart postgresql@17-main

# Allow incoming TCP traffic on port 5432
$ sudo ufw allow 5432/tcp
Enter fullscreen mode Exit fullscreen mode

Note: if your not with root user you might have to enable rule from your VM console

VoilĂ ! You've just got your personal cloud-based database, which you can use anywhere with
DATABASE_URL="postgresql://twohype_user:yourpassword@YOUR_VM_IP:5432/twohype"