Back to Homepage
Wednesday 7 August 2024
15

⚒️ How to install PostgreSQL on Ubuntu ⚒️

Step 1: Update Your Package List

It's always good practice to update your package list before installing new software. Open a terminal and run:

sudo apt update

Step 2: Install PostgreSQL

You can install PostgreSQL and its associated packages using the following command:

sudo apt install postgresql postgresql-contrib

This command installs the PostgreSQL server along with some useful additional packages (like postgresql-contrib).

Step 3: Verify PostgreSQL Installation

After the installation is complete, you can verify that PostgreSQL is installed and running. Use the following command to check the status of the PostgreSQL service:

sudo systemctl status postgresql

You should see output indicating that the PostgreSQL service is active and running.

Step 4: Switch to the PostgreSQL User

PostgreSQL creates a default user named postgres. You need to switch to this user to perform administrative tasks:

sudo -i -u postgres

Now, you are logged in as the postgres user.

Step 5: Access the PostgreSQL Command Line Interface

To start using PostgreSQL, access its command line interface (CLI) by typing:

psql

This command will log you into the PostgreSQL prompt as the postgres user. You should see the PostgreSQL prompt:

postgres=#

Step 6: Create a New Database

To create a new database, use the following SQL command:

CREATE DATABASE mydatabase;

Replace mydatabase with your desired database name.

Step 7: Create a New User

You can create a new PostgreSQL user with the following command:

CREATE USER myuser WITH PASSWORD 'mypassword';

Replace myuser with your desired username and mypassword with a strong password.

Step 8: Grant Privileges

Grant the new user privileges on the new database:

GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;

Replace mydatabase and myuser with your database name and username, respectively.

Step 9: Exit the PostgreSQL Prompt

To exit the PostgreSQL prompt, type:

\q

Step 10: Test the Connection

To ensure everything is set up correctly, you can switch to the new user and connect to the new database. First, exit the postgres user:

exit

Then, connect to the new database using the new user:

psql -U myuser -d mydatabase

Replace myuser and mydatabase with your username and database name. You will be prompted to enter the password for the new user.

Configuring PostgreSQL

Allowing Remote Connections

By default, PostgreSQL only allows connections from the localhost. To allow remote connections, you need to configure PostgreSQL to listen on all IP addresses and modify the host-based authentication (HBA) file.

Step 1: Edit the PostgreSQL Configuration File

Open the postgresql.conf file:

sudo nano /etc/postgresql/12/main/postgresql.conf

Find the line that starts with #listen_addresses and change it to:

listen_addresses = '*'

Save and close the file.

Step 2: Edit the pg_hba.conf File

Open the pg_hba.conf file:

sudo nano /etc/postgresql/12/main/pg_hba.conf

Add the following line to allow connections from any IP address:

host    all             all             0.0.0.0/0               md5

Save and close the file.

Step 3: Restart PostgreSQL

For the changes to take effect, restart the PostgreSQL service:

sudo systemctl restart postgresql

Enabling SSL for Secure Connections

For secure remote connections, it's recommended to enable SSL. Here's a brief overview:

1. Generate SSL Certificates: Create a server key and certificate.

2. Configure PostgreSQL to Use SSL: Edit the postgresql.conf file to point to the SSL certificate and key files.

3. Restart PostgreSQL: Apply the changes by restarting the PostgreSQL service.

Refer to the PostgreSQL documentation for detailed steps on enabling SSL.

Share:
Created by:
Author photo

Jorge García

Fullstack developer