How to Shrink (Downsize) The Droplate Size

TLDR

You Can’t.

But in our world this is just a saying. Everything is possible.

How to Actually Do It?

Ok, let’s move to explanation. You actually cannot shrink an existing droplet size. That would be going down in price tier and would tear the server apart. Since as they say it actually is not safe because:

Data is not always sequentially written in memory, so reducing the available space would risk data loss and filesystem corruption

I was having a 20$ droplet from times when I was saving audio in colorful barcodes and doing other image heavy processing so I needed more power. Now I just want to keep my DB, learn NextJS and do incremental changes to my Django server. So what can we do?

I was certain that Deploying Existing Snapshots to a new Droplet that would be smaller would be a solution. And after actually cleaning up around 10 GB of my data I was ready but… it is not possible to go back to a smaller droplet size even if the actual disk size fits below new droplet capacity.

So what can we do?

Migrate.

Fortunately in my case it was just deploying the Django App to a new droplet and copying database - I had PostgreSQL with postgis extension.

Let’s Migrate PostgreSQL Database

The scenario I did is not the quickest but safest and easiest to do in my opinion. It is as follows: We use our computer to connect with server1, we make backup, download it to out computer, then we connect to server2 and perform data load.

We need to prepare a couple of things besides the 2 servers we want to migrate the data between.

  1. FileZilla
  2. Port 22 on both servers
  3. Magic commands (down below)
  4. Users with admin permissions on both databases.

Step 1

  • Log in to server 1 (via password or ssh)
  • Enter the PSQL shell with:
sudo -u postgres psql
  • Create DB backup with:
sudo -u your_admin_user pg_dump your_db_name > the_backup.sql

(user should have permissions and ownership over the db)

Step 2

  • Log into FileZilla and using it:
  • Connect to Server 1 (the one your connected to to do the dump)
  • Download the created the_backup.sql file to your machine.
  • Connect to Server 2.

Step 3

Create a clean database with:

CREATE DATABASE second_db_name;

Give your user all privilages over it.

GRANT ALL PRIVILEGES ON DATABASE second_db_name TO second_admin_user;

Perform the backup load.

sudo -u second_admin_user psql second_db_name < the_backup.sql

Remember to delete the_backup.sql file from all places afterwards.

Other ways

Other ways of doing it would be:

  • Using rsync to copy whole part of disk to another folder/partition where the DB is located at. This could be done if the size to migrate is big. It can be done over the network and to a server in a different location too.
  • Using pg_dump to transfer data directly to another server´s DB over the network. That is in my option more complicated and less robust.

Hope it helped. Ping me if you need to know more.


Tell me about your insights and leave a comment - you are most welcome to see more posts of this type just go to home page

Oh, what is that? No newsletter?

Freeing you from mailing list is my first step of helping the world to be a better place.