DEV Community

Loi2008
Loi2008

Posted on • Edited on

A Guide for Creating a Linux Server VM on Azure & Installing PostgreSQL

This guide covers:

  1. Creating a Linux VM on Azure
  2. Installing PostgreSQL
  3. Configuring PostgreSQL for local and remote access

โœ… Prerequisites

  • Active Azure subscription
  • Access to Azure Portal
  • SSH client (Linux on Windows)

๐Ÿงฐ Part 1: Create a Linux VM on Azure

1. Go to Azure Portal

2. Configure VM Basics

  • Subscription: Choose your active subscription ( subscribe if you do not have an active subscription)
  • Resource Group: Create/select one
  • VM Name: e.g., linux-postgres-vm
  • Region: Closest to your location
  • Image: Ubuntu 22.04 LTS
  • Size: e.g., Standard B1s
  • Authentication: - SSH public key (recommended)
    • Password:e.g, 1234
  • Username: e.g., myLinuxServer
  • SSH public key: Paste your public key if using SSH

    You can generate one using:

    ssh-keygen -t rsa -b 2048
    

3. Networking

  • Public IP: Enabled
  • NSG (firewall): Allow SSH (port 22)

4. Create the VM

  • Click "Review + create" then "Create"

๐Ÿ”Œ Part 2: SSH into the VM

ssh azureuser@<your-vm-public-ip- e.g, 192.168.20.139>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ˜ Part 3: Install PostgreSQL

1. Update Packages

sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

2. Install PostgreSQL

sudo apt install postgresql postgresql-contrib -y
Enter fullscreen mode Exit fullscreen mode

3. Enable and Start the Service

sudo systemctl enable postgresql
sudo systemctl start postgresql
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Part 4: Configure PostgreSQL

1. Switch to postgres User

sudo -i -u postgres
Enter fullscreen mode Exit fullscreen mode

2. Access PostgreSQL Terminal

psql
Enter fullscreen mode Exit fullscreen mode

3. Create User and Database

CREATE ROLE e.g, myPostgres WITH LOGIN PASSWORD e.g, '1234';
ALTER ROLE myPostgres CREATEDB;
CREATE DATABASE student OWNER myPostgres;
\q
exit
Enter fullscreen mode Exit fullscreen mode

๐ŸŒ Part 5: Allow Remote Access (Optional)

1. Edit postgresql.conf

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

Find:

listen_addresses = 'localhost'
Enter fullscreen mode Exit fullscreen mode

Change to:

listen_addresses = '*'
Enter fullscreen mode Exit fullscreen mode

2. Edit pg_hba.conf

sudo nano /etc/postgresql/15/main/pg_hba.conf
Enter fullscreen mode Exit fullscreen mode

Add this line:

host    all             all             0.0.0.0/0               md5
Enter fullscreen mode Exit fullscreen mode

3. Allow Port 5432 in Azure NSG

  • Go to Azure portal > VM > Networking
  • Click "Add inbound port rule"
    • Port: 5432
    • Protocol: TCP
    • Action: Allow

4. Restart PostgreSQL

sudo systemctl restart postgresql
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช Part 6: Connect Remotely

From your local pc:

psql -U myPostgres -d student -h <192.168.20.139> -p 5432
Enter fullscreen mode Exit fullscreen mode

Or use GUI tools such as DBeaver.


Your Linux VM is now running PostgreSQL and ready to accept connections.

Top comments (0)