@88um
Published September 20, 2024
An Ubuntu system (this guide uses Ubuntu 20.04 LTS, but should work for other recent versions)
Sudo privileges on your Ubuntu system
Basic familiarity with the command line
PostgreSQL (postgres) is a powerful open-source relational database system supporting a wide array of features including:
ACID Compliance: Ensures reliable transactions.
Complex Queries: Allows for sophisticated SQL queries, including joins, subqueries, and window functions.
Extensibility: You can define custom data types, functions, and operators.
Support for JSON: Handles unstructured data with robust JSON support. (Love this feature)
Concurrency: Uses Multiversion Concurrency Control (MVCC) to manage multiple transactions simultaneously without locking.
The choice of a database system depends on your overall projects needs and requirements. Since I store different types of JSON data and Enums related to this website, I have deemed both SQLite and MySQL inadequate in meeting my storage needs. Since the beginning, this website's data has been stored in a free PostgreSQL database provided by Supabase which hosts my data on AWS cloud. Supabase also provides services on top of the hosted database, such as:
Authentication: Easy user authentication and authorization options.
APIs: Automatically generated RESTful and GraphQL APIs for interacting with your database.
Storage: File storage for managing media and other assets.
Auto scaling: Your database grows with your data allowing for more flexibility
Real-time Subscriptions: Allows applications to receive real-time updates when data changes.
Typically, I would never self-host my databases since managing, keeping it secure, and scaling the database could become a chore. Unfortunately, nearly all of this website's article data vanished from Supabase's servers (likely due to inactivity), which is why I purchased a $4 Linux VPS for the sole purpose of hosting my own data. I found this method feasible since I do not expect to scale anytime in the future or fuss too hard at managing it. With that being said, let's begin.
Before installing any new software, it's a good practice to update your system:
sudo apt update sudo apt upgrade -y
Install PostgreSQL and the contrib
package, which provides some additional utilities and functionality:
sudo apt install postgresql postgresql-contrib -y
After the installation completes, PostgreSQL should start automatically. Verify the installation and service status:
sudo systemctl status postgresql
You should see output indicating that the service is active and running.
By default, PostgreSQL creates a user account called postgres
during installation. Switch to this account:
sudo -i -u postgres
You can now access the PostgreSQL prompt by typing:
psql
To exit the PostgreSQL prompt, type:
\q
It's generally not a good practice to use the default postgres
user for your applications. Let's create a new database and user:
Access the PostgreSQL prompt:
sudo -u postgres psql
Create a new database:
CREATE DATABASE myapp;
Create a new user:
CREATE USER myapp_user WITH PASSWORD 'your_password';
Grant privileges to the new user on the new database:
GRANT ALL PRIVILEGES ON DATABASE myapp TO myapp_user;
Exit the PostgreSQL prompt:
\q
By default, PostgreSQL only allows connections from localhost. If you need to allow remote connections (such as with my case):
Edit the PostgreSQL configuration file:
sudo nano /etc/postgresql/12/main/postgresql.conf
(Replace "12" with your postgres version)
Find the line that says listen_addresses
, uncomment it, and change it to:
listen_addresses = '*'
Edit the client authentication configuration file:
sudo nano /etc/postgresql/12/main/pg_hba.conf
Add the following line at the end of the file to allow connections from any IP address:
host all all 0.0.0.0/0 md5
Note: This allows connections from any IP. In a production environment, you should restrict this to specific IP addresses or ranges.
Restart PostgreSQL to apply the changes:
sudo systemctl restart postgresql
Use strong passwords for all database users.
Regularly update PostgreSQL to the latest version.
Configure your firewall to only allow connections on the PostgreSQL port (default 5432) from trusted IP addresses.
Consider using SSL for encrypted connections (Tutorial on this later?).
Okay great. Now, you and I both have a PostgreSQL database installed and running on your Ubuntu system. You've created a new database and user, and configured it for remote access. Yes, this is a simple guide, but if you are looking for more detailed information you can always read the docs...but this will do for now at least for my purposes lol