Can I delete $PGDATA after disabling postgresql?

I was running postgresql as a service on my NixOS machine.

$ grep postgres /etc/nixos/configuration.nix
  services.postgresql.enable = true;

The data lived in /var/lib/postgresql/9.6.

$ systemctl cat postgresql.service | grep PGDATA

I no longer need to run postgresql, so I’ve disabled (removed) the service in my nixos config.
I don’t care about the data, so I’d like to delete the /var/lib/postgresql/9.6 directory as well.

Is this the correct way to remove the remnants of postgresql from my system?
In other words, is it safe to delete and are there other locations I should check for “leftover” postgresql files/directories?


But something may be left in /nix/store. You should do aggressive garbage collection of previous system generations to remove all

postgresql.conf and pg_hba.conf files. Also, in some configurations PG leaves files in /tmp.

Yes, /var/lib/postgresql is where PostgreSQL stores its state. So if you don’t need the data anymore it’s safe to remove it.

Once you’ve disabled PostgreSQL and have done a nix-collect-garbage -d your system won’t contain any remnants of PostgreSQL.

@danbst @basvandijk Thanks, that’s exactly what I needed to know.