Setup - Ubuntu

1

To set up PostgreSQL on Ubuntu, follow these steps:

1. Update the System

sudo apt update
sudo apt upgrade -y

2. Install PostgreSQL

sudo apt install postgresql postgresql-contrib -y

3. Start and Enable the PostgreSQL Service

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:

sudo -i -u postgres

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

psql

You should now see the psql prompt:

postgres=#

6. Create a New Database

At the psql prompt, run:

7. Create a New User

8. Grant Privileges

9. Exit psql

Type:

10. Allow Remote Connections (Optional)

To allow remote connections to PostgreSQL:

  1. Edit the PostgreSQL configuration file:

    Locate listen_addresses and change it to:

  2. Edit the pg_hba.conf file:

    Add a line like this for IPv4 connections:

  3. Restart PostgreSQL:

11. Test the Setup

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

Enter the password when prompted.

12. Install Additional Tools (Optional)

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

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

  2. Add the PostgreSQL APT Repository

  3. Update Repositories

  4. Install pgAdmin4 For the desktop version:

    For the web version:

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

    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


2. Insert Data into the tuser Table (Create)


3. Retrieve Data from the tuser Table (Read)

  • Retrieve All Records:

  • Retrieve Specific Columns:

  • Retrieve Data with a Condition:


4. Update Data in the tuser Table (Update)

  • Update a Specific Record:

  • Update Multiple Fields:


5. Delete Data from the tuser Table (Delete)

  • Delete a Specific Record:

  • Delete All Records:


6. Drop the tuser Table

If you need to remove the table entirely:

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