--- Classes: - IT Foundations --- # Postgresql Postgresql is a popular open-source relational database, and is widely available on Linux. **Note:** A graphical interface for postgresql is something that will need to be installed separately, the only way to interact with the database that is installed by default is the `psql` command. To install the pgAdmin GUI program see [pgAdmin](#pgadmin) below. ## Install All linux distributions package postgresql and you can just install the relevant package. Some common examples are below: ### Arch ```sh sudo pacman -S postgresql ``` ### Debian/Mint/Ubuntu ```sh sudo apt install postgresql ``` ### Fedora ```sh sudo dnf install postgresql-server # Initialise Server Data sudo /usr/bin/postgresql-setup --initdb ``` ## Setup First make sure postgres is running: ```sh systemctl status postgresql.service ``` If not, start it with: ```sh sudo systemctl start postgresql.service ``` If you want it to start on boot, you can enable it with: ```sh sudo systemctl enable postgresql.service ``` To stop it from starting on boot, replace enable with disable. Initially the postgres database user will not have a password to allow access. To access the database run the following: ```sh sudo -u postgres psql ``` From here you can do initial setup. For a simple setup where you just want to access from a client like pgAdmin, you can set a password for the `postgres` user with the following query: ```sql ALTER USER postgres WITH PASSWORD 'new_password'; ``` When done you can exit with the `\q` command. # pgAdmin pgAdmin is a graphical interface to configure and query postgresql. ## Install Most linux distributions don't include pgAdmin from their default repositories, but we can either use pgAdmin's official repositories, or it is available as a flatpak: ### Flatpak (universal) If you wish to install through flatpak ensure flatpak is setup, see [Flatpak](Flatpak.md) for details if not. If you have a graphical software manager/store it should be available there, to install via command-line: ```sh flatpak install org.pgadmin.pgadmin4 ``` ### Arch (AUR Package) ```sh yay -S pgadmin4-desktop-bin ``` ### Debian/Ubuntu ```sh # Install the public key for the repository (if not done previously): curl -fsS https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/packages-pgadmin-org.gpg # Create the repository configuration file: sudo echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list # Refresh package list apt update # Install pgAdmin sudo apt install pgadmin4-desktop libnss3 libgbm-dev libasound2-dev ``` ### Fedora ```sh # Add the pgadmin repository sudo rpm -i https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-fedora-repo-2-1.noarch.rpm # Install pgAdmin sudo yum install pgadmin4-desktop ``` ### Linux Mint ```sh # Install the public key for the repository (if not done previously): curl -fsS https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/packages-pgadmin-org.gpg # Create the repository configuration file: echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/noble pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list # Refresh package list apt update # Install pgAdmin sudo apt install pgadmin4-desktop libnss3 libgbm-dev libasound2-dev ``` ## Setup First you will need to register your running server: ![Register Server](Screenshots/psql-register-server.png) In the General tab enter a name for your postgres server, then in the connection tab enter `localhost` for Hostname/address and enter the password for the postgres user: ![Postgres Connection](Screenshots/psql-connection.png) Now click save and pgAdmin should be ready to use as normal!