Skip to content
How to Uninstall PostgreSQL (Ubuntu, macOS, Windows)

Click to use (opens in a new tab)

How to Uninstall PostgreSQL (Ubuntu, macOS, Windows)

August 26, 2026 by Chat2DBChat2DB Team

Removing PostgreSQL is not just apt remove postgresql. That command leaves the data directory, the configuration, the postgres system user and — most annoyingly — the old cluster registration, which makes a later reinstall fail or silently pick up a database you thought was gone. This guide covers a genuinely complete removal on each platform, plus how to verify it worked.

Before you start: back up

Uninstalling deletes your databases irrecoverably. Thirty seconds of insurance:

# Everything, including roles and tablespaces
pg_dumpall -U postgres -f ~/postgres_backup_$(date +%Y%m%d).sql
 
# Or one database in the compressed custom format
pg_dump -U postgres -Fc -d mydb -f ~/mydb_$(date +%Y%m%d).dump

Copy the file somewhere off the machine, then verify it is not empty and starts with SQL:

ls -lh ~/postgres_backup_*.sql
head -20 ~/postgres_backup_*.sql

Restoring later is psql -f backup.sql postgres for a pg_dumpall file, or pg_restore -d mydb file.dump for a custom-format dump.

Ubuntu and Debian

1. Find what is installed

dpkg -l | grep -i postgres

You will typically see postgresql-16, postgresql-client-16, postgresql-common, postgresql-client-common, and often postgresql-contrib. Note the version numbers.

2. Stop the service

sudo systemctl stop postgresql
sudo systemctl disable postgresql

3. Purge the packages

remove keeps configuration files; purge deletes them. You want purge:

sudo apt purge -y 'postgresql*' 'postgresql-client*' 'postgresql-common' 'postgresql-client-common'
sudo apt autoremove -y --purge

If the glob does not match because of how your shell handles it, quote it as above or list packages explicitly:

sudo apt purge -y postgresql-16 postgresql-client-16 postgresql-contrib-16 postgresql-common

postgresql-common is the one people forget. It owns the cluster registry in /etc/postgresql-common/ and the pg_ctlcluster wrappers, and leaving it behind is the usual cause of a reinstall that behaves strangely.

4. Delete the data and config directories

Purging packages does not remove /var/lib/postgresql — Debian packaging deliberately protects your data.

sudo rm -rf /var/lib/postgresql/     # ← your databases live here
sudo rm -rf /etc/postgresql/
sudo rm -rf /etc/postgresql-common/
sudo rm -rf /var/log/postgresql/
sudo rm -rf /var/run/postgresql/

Look before you delete. sudo du -sh /var/lib/postgresql/ tells you how much data is about to disappear; if that number surprises you, go back to the backup step.

5. Remove the system user and group

sudo userdel -r postgres    # -r also removes its home directory
sudo groupdel postgres

userdel -r may warn that the home directory does not exist if you already deleted /var/lib/postgresql. That warning is harmless.

6. Remove the APT repository, if you added one

If you installed from the PostgreSQL Global Development Group repository rather than Ubuntu's own:

sudo rm -f /etc/apt/sources.list.d/pgdg.list
sudo rm -f /etc/apt/trusted.gpg.d/postgresql.gpg
sudo rm -f /usr/share/keyrings/postgresql-keyring.gpg
sudo apt update

7. Verify

which psql postgres pg_ctl        # should print nothing
dpkg -l | grep -i postgres        # should print nothing
ls /var/lib/postgresql 2>&1       # No such file or directory
sudo ss -lntp | grep 5432         # nothing listening
id postgres 2>&1                  # no such user

Five clean results means you are done.

RHEL, CentOS, Rocky and Fedora

sudo systemctl stop postgresql-16
sudo systemctl disable postgresql-16
 
sudo dnf remove -y 'postgresql*'
# or: sudo yum remove -y 'postgresql*'
 
sudo rm -rf /var/lib/pgsql/
sudo rm -rf /usr/pgsql-16/
sudo rm -f  /etc/systemd/system/postgresql-16.service
sudo systemctl daemon-reload
 
sudo userdel -r postgres
sudo groupdel postgres

Note the different data directory: RPM-based distributions use /var/lib/pgsql/<version>/data, not /var/lib/postgresql.

macOS

Which method you use depends on how you installed it.

Homebrew

brew services stop postgresql@16
brew uninstall postgresql@16
brew uninstall --force postgresql       # any other formula versions
 
# Data is not removed by uninstall:
rm -rf /opt/homebrew/var/postgresql@16      # Apple Silicon
rm -rf /usr/local/var/postgresql@16         # Intel
rm -rf /opt/homebrew/var/postgres           # older formula layout
 
# Leftover launch agents
rm -f ~/Library/LaunchAgents/homebrew.mxcl.postgresql*.plist
 
brew cleanup

Postgres.app

osascript -e 'quit app "Postgres"'
rm -rf /Applications/Postgres.app
rm -rf ~/Library/Application\ Support/Postgres
rm -rf ~/Library/Preferences/com.postgresapp.Postgres*.plist
rm -rf ~/Library/Saved\ Application\ State/com.postgresapp.Postgres.savedState

If you added Postgres.app's binaries to your PATH, remove that line from ~/.zshrc or ~/.bash_profile:

grep -n "Postgres.app" ~/.zshrc ~/.bash_profile 2>/dev/null

EnterpriseDB installer

The graphical installer ships its own uninstaller:

sudo /Library/PostgreSQL/16/uninstall-postgresql.app/Contents/MacOS/installbuilder.sh

Then clean up what it leaves:

sudo rm -rf /Library/PostgreSQL
sudo rm -f  /etc/postgres-reg.ini
sudo rm -rf /Library/LaunchDaemons/com.edb.launchd.postgresql-16.plist
sudo dscl . -delete /Users/postgres
sudo dscl . -delete /Groups/postgres

Verify on macOS

which psql postgres
ls /Library/PostgreSQL 2>&1
lsof -nP -iTCP:5432 -sTCP:LISTEN

Windows

Using the installer

  1. Open Settings → Apps → Installed apps, find PostgreSQL 16, choose Uninstall.
  2. Or run the bundled uninstaller directly: C:\Program Files\PostgreSQL\16\uninstall-postgresql.exe

The uninstaller asks whether to remove the data directory. If you say no — or if it fails partway — clean up manually:

# Stop and delete the service
Stop-Service postgresql-x64-16
sc.exe delete postgresql-x64-16
 
# Remove the remaining directories
Remove-Item -Recurse -Force "C:\Program Files\PostgreSQL"
Remove-Item -Recurse -Force "C:\Program Files (x86)\PostgreSQL"
 
# Registry keys
Remove-Item -Recurse -Force "HKLM:\SOFTWARE\PostgreSQL"

Then remove C:\Program Files\PostgreSQL\16\bin from the system PATH (System Properties → Environment Variables), and delete the postgres Windows user account if the installer created one (Computer Management → Local Users and Groups).

Verify on Windows

Get-Service | Where-Object {$_.Name -like "*postgres*"}
Get-NetTCPConnection -LocalPort 5432 -ErrorAction SilentlyContinue

Common problems

"Package is in a very bad inconsistent state" on apt. Force the removal, then retry:

sudo dpkg --remove --force-remove-reinstreq postgresql-16
sudo apt --fix-broken install

Port 5432 still in use after uninstall. Something else is bound to it — often a Docker container or a second installation:

sudo lsof -nP -iTCP:5432 -sTCP:LISTEN
docker ps --filter "publish=5432"

Reinstall picks up the old databases. You removed the packages but not /var/lib/postgresql. The new install finds the existing cluster and adopts it. Delete the data directory and reinstall.

initdb fails after reinstall with a locale error. Leftover /etc/postgresql-common/createcluster.conf is applying old settings. Remove it and reinstall postgresql-common.

Homebrew says the formula is still linked. brew uninstall --force postgresql@16 && brew cleanup --prune=all.

If you only wanted to start over

A full uninstall is overkill if the real goal is a clean database. Dropping and recreating is faster and reversible:

DROP DATABASE mydb;
CREATE DATABASE mydb OWNER myuser;

Or reset just the schema, keeping the database and its grants:

DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO PUBLIC;

On Debian/Ubuntu you can also recreate a whole cluster from scratch without touching packages:

sudo pg_dropcluster --stop 16 main
sudo pg_createcluster --start 16 main

And if the reason for uninstalling is that a heavyweight admin tool came bundled with your server install, you can remove that separately and keep the database. Chat2DB (opens in a new tab) is a free, lightweight AI-powered SQL client that connects to PostgreSQL and 20+ other databases without installing anything server-side — there is even a browser version at app.chat2db.ai (opens in a new tab) if you would rather install nothing at all.

Summary

PlatformPackagesData directoryUser
Ubuntu/Debianapt purge 'postgresql*' postgresql-common/var/lib/postgresql, /etc/postgresqluserdel -r postgres
RHEL/Rockydnf remove 'postgresql*'/var/lib/pgsql, /usr/pgsql-*userdel -r postgres
macOS (brew)brew uninstall postgresql@16$(brew --prefix)/var/postgresql@16n/a
macOS (EDB)uninstall-postgresql.app/Library/PostgreSQLdscl . -delete /Users/postgres
Windowsuninstall-postgresql.exeC:\Program Files\PostgreSQLLocal Users and Groups

The step people skip on every platform is the data directory, and it is the one that causes the confusing reinstall behaviour later. Back up first, delete it deliberately, then run the verification commands.