Install and Uninstall PostgreSQL in Debian
In this tutorial you will learn how to install and uninstall PostgreSQL database server on Debian and other Linux machines.
In this tutorial, you will learn how to install, test, use, and completely uninstall the PostgreSQL database server on Debian-based Linux systems. The steps covered here apply to Debian, Ubuntu, and other compatible distributions, and are suitable for both development and production environments.
Updating the System Package Index
Before installing PostgreSQL, it is important to ensure that your system is aware of the latest available packages. Updating the package index helps prevent dependency issues and guarantees that you install the most recent stable version provided by your distribution.
sudo apt update
Installing PostgreSQL on Linux
PostgreSQL and its commonly used extensions can be installed directly from the official Debian repositories. The postgresql package installs the database server, while postgresql-contrib provides additional tools and utilities. The -y flag automatically confirms the installation.
sudo apt install postgresql postgresql-contrib -y
Enabling and Starting the PostgreSQL Service
After installation, PostgreSQL should be started immediately and configured to launch automatically at system boot. This ensures the database server is always available without manual intervention.
sudo systemctl enable --now postgresql
To verify that the PostgreSQL service is running correctly and is in a healthy state, you can check its status using systemd.
systemctl status postgresql
Testing and Using PostgreSQL
PostgreSQL creates a default administrative system user named postgres. Switching to this user allows you to access the database server with full administrative privileges.
sudo -iu postgres
Once logged in as the postgres user, start the PostgreSQL interactive terminal by launching psql. This interface allows you to execute SQL commands and manage databases.
psql
To confirm that PostgreSQL is working correctly, you can check the server version and list existing databases.
SELECT version();
\\l
Exit the PostgreSQL interactive shell using \\q, and then return to your regular system user by typing exit.
Uninstalling PostgreSQL from Linux
If PostgreSQL is no longer needed, it can be completely removed from the system. Before uninstalling, ensure that the service is stopped to avoid conflicts or data corruption.
sudo systemctl stop postgresql
To remove PostgreSQL and all related packages installed via APT, use the purge option. This removes both the software and its configuration files.
sudo apt purge postgresql 'postgresql-*' -y
After purging the main packages, it is recommended to remove any unused dependencies that were installed automatically.
sudo apt autoremove --purge -y
Removing PostgreSQL Data and Configuration Files
For a complete cleanup, including all databases, user data, and configuration files, you can manually delete PostgreSQL’s data and configuration directories. Only perform this step if you are certain the data is no longer needed.
sudo rm -rf /var/lib/postgresql
sudo rm -rf /etc/postgresql
Optionally, you may also remove the PostgreSQL system user and group. Do this only if you do not plan to reinstall PostgreSQL on the same machine.
sudo deluser postgres && sudo delgroup postgres
Conclusion
You have now learned how to install PostgreSQL on Debian-based Linux systems, verify that it is running correctly, use the psql interface, and fully remove PostgreSQL when it is no longer required. These steps provide a clean and reliable workflow for managing PostgreSQL in both testing and production environments.
Subscribe to Our YouTube for More
https://blog.arashtad.com/linux/install-and-uninstall-postgresql-in-debian/?feed_id=13784&_unique_id=6946fe6be70eb
Comments
Post a Comment