Published on

Self Hosted PostgreSQL with Authorization, Login, and Security

Authors

if doing self hosting how to do authorization login and security

Self-Hosting PostgreSQL with Authorization, Login, and Security

When self-hosting a PostgreSQL database for your desktop and mobile apps to share invoice data for electrical motor repairs, implementing authorization, login, and security is critical to protect your data and ensure only authorized users (e.g., employees) can access or modify it. Since your apps need to read and write to the same database, you’ll need a secure setup that works across platforms. This guide outlines how to achieve this with a self-hosted PostgreSQL instance, using a backend API for simplicity and security, tailored to your small company’s needs.


Architecture Overview

  1. PostgreSQL Database: Self-hosted on a server (e.g., VPS or office PC).
  2. Backend API: A Kotlin/Ktor server to manage authentication and database access (direct database connections from apps are risky).
  3. Apps: Kotlin Multiplatform (KMP) desktop and mobile apps interacting with the API, not the database directly.
  4. Security Layers: User authentication (login), API authorization, and network/database security.

This approach avoids exposing PostgreSQL to the internet directly, which is safer and easier to manage than configuring database-level access for each app.


Step-by-Step Implementation

1. Start the PostgreSQL service

brew services start postgresql@14

2. Verify the installation by connecting to the default databas

 psql postgres

3. Configure a New Connection

  1. Open the Command Palette:
    Press ⌘+Shift+P (on macOS) or Ctrl+Shift+P (on Windows/Linux) to bring up the Command Palette. Type "PostgreSQL: New Connection" (or a similar command as specified in the extension’s documentation) and select it.

  2. Provide Connection Details:
    You will be prompted to enter your PostgreSQL connection information, which typically includes:

    • Host: Usually localhost if you're running PostgreSQL locally
    • Port: Default is 5432
    • Database: Enter your initial database (often postgres, unless you have another default)
    • Username: patriciadumontier
    • Password: The computer login password