Thapa Technical — Dev Blog
MySQL PostgreSQL MongoDB VPS Ubuntu

Installing Databases on a VPS Server — MySQL, PostgreSQL & MongoDB.

Your backend is only as good as the database behind it. On a VPS you're not stuck with whatever a shared host hands you — you install exactly what your app needs. This guide walks you through setting up the three databases you'll actually reach for: MySQL, PostgreSQL and MongoDB — installing, securing, creating users, and getting a ready-to-paste connection string for each. Pick one, or install all three. No prior server experience required.

What You'll Need

Everything below assumes a fresh Ubuntu VPS (22.04 or 24.04 LTS). The commands are copy-paste ready — just change the database name, username and password to your own.

  • A VPS running Ubuntu and its public IP address
  • SSH access — usually root or a sudo-enabled user
  • 5–10 minutes per database
Don't have a VPS yet? Grab one for up to 80% off — plus an extra 10% with my Hostinger deal just below (code THAPA7), then come straight back to this step.
Exclusive Reader Deal
STACKABLE — LINK + CODE
Thapa Technical × Hostinger

Grab a Powerful VPS for up to 80% OFF
+ an extra 10% OFF with my code 🔥

Most providers charge ₹1,500–₹2,000+ a month for a real VPS. Through my link you get the same horsepower — dedicated CPU & RAM, full root access, NVMe SSD — for a fraction of that. Then stack code THAPA7 on top for an extra 10% off the already-discounted price. That's a discount on a discount — cheaper, never weaker.

Typical price elsewhere
₹1,799/mo
other providers, billed monthly
BEST VALUE
My link + code THAPA7
₹359/mo
up to 80% off — then 10% more
Extra 10% OFF code THAPA7
  • Full root / SSH access — install any DB on this page
  • Dedicated RAM & CPU + lightning NVMe SSD storage
  • Free domain, dedicated IP, weekly backups & AI assistant
  • 30-day money-back guarantee — zero risk
1 Open my link 2 Pick a VPS plan 3 Apply code THAPA7 at checkout

Affiliate link — you pay the same (or less with code THAPA7), and it supports these free tutorials. 💚

1
Connect

SSH In & Update the Server

Log into your VPS over SSH using its IP address (or domain, if you've pointed one):

bash — local machine
ssh root@your_server_ip

Always refresh the package list before installing anything so you pull the latest versions:

bash — on your VPS
sudo apt update && sudo apt upgrade -y
Check your Ubuntu version

A couple of steps below depend on your release. Run lsb_release -ajammy means Ubuntu 22.04 and noble means 24.04. You'll need that codename for MongoDB.

2
MySQL

Install & Secure MySQL

MySQL is the classic relational database — perfect for PHP, Laravel, Node and most CRUD apps. Install the server package:

bash — on your VPS
sudo apt install -y mysql-server

Make sure the service is running and starts on boot:

bash — on your VPS
sudo systemctl start mysql
sudo systemctl enable mysql
sudo systemctl status mysql

Now lock it down. This script lets you set password rules, remove the anonymous user, and disable remote root login — answer Y to the safe defaults:

bash — on your VPS
sudo mysql_secure_installation

Open the MySQL shell, then create a dedicated user and a database for your app:

bash — on your VPS
sudo mysql
sql — mysql prompt
CREATE DATABASE myapp_db;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'strong_password_here';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Your app connects with this string:

connection string
mysql://myapp_user:strong_password_here@localhost:3306/myapp_db
Grant privileges on myapp_db.* — just that one database — not *.*. Never give an app account access to every database on the server.
3
PostgreSQL

Install PostgreSQL & Create a Role

PostgreSQL is the go-to for modern stacks — Prisma, Drizzle, Django, Rails. Install the server and its contrib extensions:

bash — on your VPS
sudo apt install -y postgresql postgresql-contrib
sudo systemctl enable --now postgresql

PostgreSQL creates a system user called postgres. Switch to it and open the psql shell:

bash — on your VPS
sudo -i -u postgres
psql

Create a login role and a database that role owns. Run these at the postgres=# prompt:

sql — psql prompt
CREATE ROLE myapp_user WITH LOGIN PASSWORD 'strong_password_here';
CREATE DATABASE myapp_db OWNER myapp_user;
GRANT ALL PRIVILEGES ON DATABASE myapp_db TO myapp_user;
\q

Type exit to leave the postgres user. Your connection string:

connection string
postgresql://myapp_user:strong_password_here@localhost:5432/myapp_db
Tip

Giving the role ownership of its own database is safer than handing it SUPERUSER. It's everything your app needs and keeps the rest of the server out of reach. The default port is 5432.

4
MongoDB

Install MongoDB Community Edition

MongoDB isn't in Ubuntu's default repositories, so you add MongoDB's official one. First install the tools needed to import its signing key:

bash — on your VPS
sudo apt install -y gnupg curl

Import the public GPG key for the current release (8.0):

bash — on your VPS
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
  sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor

Add the repository. Match the codename to your Ubuntu version — use noble for 24.04 or jammy for 22.04:

bash — on your VPS (Ubuntu 24.04)
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | \
  sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

Refresh the package list and install MongoDB:

bash — on your VPS
sudo apt update
sudo apt install -y mongodb-org

Start the service, enable it on boot, and confirm it's up:

bash — on your VPS
sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod

Test it by opening the Mongo shell:

bash — on your VPS
mongosh

By default MongoDB listens only on localhost (port 27017), so your app connects with:

connection string
mongodb://127.0.0.1:27017/myapp_db
A fresh MongoDB has no authentication. That's fine while it only listens on localhost, but if you ever expose it, enable auth first — create an admin user and set security.authorization: enabled in /etc/mongod.conf.

The Three at a Glance

DatabaseDefault portBest for
MySQL3306PHP / Laravel, classic relational CRUD apps
PostgreSQL5432Prisma / Drizzle, Django, advanced SQL & JSON
MongoDB27017Document data, flexible schemas, MERN apps

Quick Troubleshooting

SymptomLikely fix
Service won't startCheck logs with sudo journalctl -u mysql (or mongod) for the exact error.
MongoDB install failsWrong codename in the repo line — it must match your Ubuntu version (noble / jammy).
App can't connectDouble-check the username, password and database name in your connection string.
Access denied (MySQL)Re-run the GRANT and FLUSH PRIVILEGES for the right host ('localhost').

Frequently Asked Questions

How do I install MySQL on a VPS?

Run sudo apt install -y mysql-server on Ubuntu, start and enable it with systemctl, secure it with sudo mysql_secure_installation, then create a user and database from the sudo mysql shell. MySQL listens on port 3306.

How do I install PostgreSQL on a VPS?

Install with sudo apt install -y postgresql postgresql-contrib, switch to the postgres user, open psql, and create a login role plus a database it owns. PostgreSQL listens on port 5432.

How do I install MongoDB on a VPS?

MongoDB isn't in Ubuntu's default repositories — add the official MongoDB 8.0 repo and GPG key (matching noble for 24.04 or jammy for 22.04), then sudo apt install -y mongodb-org and start mongod. It listens on port 27017.

Which database should I use — MySQL, PostgreSQL or MongoDB?

MySQL suits PHP/Laravel and classic relational apps, PostgreSQL fits modern stacks like Prisma, Drizzle and Django, and MongoDB is best for flexible document data such as MERN apps. You can run all three on the same VPS.

What are the default database ports?

MySQL 3306, PostgreSQL 5432, MongoDB 27017. By default each listens only on localhost — the safest setup for an app on the same server.

Wrapping Up

You now know how to install and secure the three databases you'll actually use in production — MySQL, PostgreSQL and MongoDB — on your own VPS, complete with users, databases and connection strings ready to drop into a .env file. From here, point your backend at the right string and you're live.

Next step: wire one of these into a real deployment. Our guide on hosting a full-stack Next.js app with PostgreSQL on a VPS picks up exactly where this leaves off.

Discussion

Leave a Reply
Ready to Ship Real Apps?
Learn Full-Stack Deployment Hands-On

Join our live online classes and learn to build and deploy production-ready full-stack apps — from database to live server — with expert mentorship.

Explore the Bootcamp
Also check: /blog/host-fullstack-nextjs-postgresql-vps.php — Next.js + PostgreSQL on a VPS