The following shouldn't be used for anything where data security is a concern. This is a proof of concept only.
Before you go down this route, just know Digital Ocean offers managed database solutions for about $15 per month which offers far better availability, data resiliency, security and pretty much everything else.
Sure, having a database run on a small droplet means the performance isn't great, but sometimes you need to run something on the cheap. This is a way to do just that.
Create a droplet
https://cloud.digitalocean.com/droplets
- Image <- Ubuntu (I used the 20.04 LTS x64)
- Shared CPU <- Basic Plan
- Block Storage <- 10GB (larger volumes cost more)
- Data center <- somewhere close to your server
- Host name <- something useful
- Cert / Password <- Password is easier for most people
You can leave everything else as it is.
SSH to the droplet
- Username: root
- Password: [created in previous step]
- Host/IP: Should be showing in the droplet overview.
Install Postgres / Postgis
apt-get update -y && apt-get upgrade -y && \
apt-get install postgis # or postgres if that is all you need
CHANGE THE POSTGRES PASSWORD!
sudo -u postgres psql # Run psql with the postgres user
# In psql, issue the change password command \password
\password postgres
# Enter new password:
# Enter it again:
Edit the configs
Stop Postgres
systemctl stop postgresql
Find the version of postgres you are running
ls /etc/postgresql # lists the version folders
# "12"
Find the /mnt/
point of your block volume
ls /mnt
# volume_lon1_01 or similar
Move the postgres data
This post was useful for pointers on moving the data.
rsync -av /var/lib/postgresql /mnt/[BLOCK VOLUME]/
Edit /etc/postgresql/[VERSION]/main/pg_hba.conf
Change:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
to:
# IPv4 local connections:
host all all 0.0.0.0/0 md5
Edit /etc/postgresql/[VERSION]/main/postgresql.conf
Change:
listen_addresses = 'localhost' # listening address
data_directory = '/var/lib/postgresql/12/main' # data
to:
listen_addresses = '*' # listen to all
data_directory = '/mnt/[BLOCK VOLUME]/postgresql/[VERSION]/main' # use block storage
Start Postgres
systemctl start postgresql
Finally, configure the droplet firewall
Never open a database to the open web without securing it first!
https://cloud.digitalocean.com/networking/firewalls
- Create a firewall
- Add the Postgres port to the firewall
- Incoming TCP Port 5432 from All Sources
Top comments (0)