Setup - Ubuntu

1

To set up PostgreSQL on Ubuntu, follow these steps:

1. Update the System

Copy

sudo apt update
sudo apt upgrade -y

2. Install PostgreSQL

Copy

sudo apt install postgresql postgresql-contrib -y

3. Start and Enable the PostgreSQL Service

Copy

sudo systemctl start postgresql
sudo systemctl enable postgresql

4. Switch to the PostgreSQL User

PostgreSQL creates a user named postgres by default. Switch to this user to manage the database:

Copy

sudo -i -u postgres

5. Access the PostgreSQL Command-Line Interface (psql)

Copy

You should now see the psql prompt:

Copy

6. Create a New Database

At the psql prompt, run:

Copy

7. Create a New User

Copy

8. Grant Privileges

Copy

9. Exit psql

Type:

Copy

10. Allow Remote Connections (Optional)

To allow remote connections to PostgreSQL:

  1. Edit the PostgreSQL configuration file:

    Copy

    Locate listen_addresses and change it to:

    Copy

  2. Edit the pg_hba.conf file:

    Copy

    Add a line like this for IPv4 connections:

    Copy

  3. Restart PostgreSQL:

    Copy

11. Test the Setup

Use the following command to log in as the new user:

Copy

Enter the password when prompted.

12. Install Additional Tools (Optional)

Install pgAdmin or any other PostgreSQL GUI tool for easier management:

Copy

Notes

  • Replace <version> with the installed PostgreSQL version, e.g., 14 or 15.

  • Use strong passwords for database users in production environments.


2

pgadmin4

The error indicates that pgadmin4 is not available in the default Ubuntu repositories. To install pgAdmin4, you need to add the official pgAdmin repository to your system.

Steps to Install pgAdmin4 on Ubuntu

  1. Update System Packages

    Copy

  2. Add the PostgreSQL APT Repository

    Copy

  3. Update Repositories

    Copy

  4. Install pgAdmin4 For the desktop version:

    Copy

    For the web version:

    Copy

  5. Configure pgAdmin4 (for Web Version) If you installed the web version, configure it:

    Copy

    Follow the prompts to set up the admin user and configuration.

  6. Access pgAdmin4

    • Desktop Version: Launch it from your system's application menu.

    • Web Version: Open your browser and go to http://<server-ip>/pgadmin4 (usually http://localhost/pgadmin4).

Troubleshooting

  • If pgAdmin4 is still unavailable for your Ubuntu version, verify that your Ubuntu distribution is supported by pgAdmin. Check pgAdmin's official documentation for further instructions.


3

CRUD

Here’s a simple set of CRUD (Create, Read, Update, Delete) SQL commands for a tuser table in PostgreSQL:

1. Create the tuser Table

Copy


2. Insert Data into the tuser Table (Create)

Copy


3. Retrieve Data from the tuser Table (Read)

  • Retrieve All Records:

    Copy

  • Retrieve Specific Columns:

    Copy

  • Retrieve Data with a Condition:

    Copy


4. Update Data in the tuser Table (Update)

  • Update a Specific Record:

    Copy

  • Update Multiple Fields:

    Copy


5. Delete Data from the tuser Table (Delete)

  • Delete a Specific Record:

    Copy

  • Delete All Records:

    Copy


6. Drop the tuser Table

If you need to remove the table entirely:

Copy

These commands cover basic operations you might perform on the tuser table. Let me know if you need more advanced queries or examples!


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


Last updated