Setup - Ubuntu
1
To set up PostgreSQL on Ubuntu, follow these steps:
1. Update the System
sudo apt update
sudo apt upgrade -y2. Install PostgreSQL
sudo apt install postgresql postgresql-contrib -y3. Start and Enable the PostgreSQL Service
sudo systemctl start postgresql
sudo systemctl enable postgresql4. Switch to the PostgreSQL User
PostgreSQL creates a user named postgres by default. Switch to this user to manage the database:
sudo -i -u postgres5. Access the PostgreSQL Command-Line Interface (psql)
psqlYou 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
psqlType:
10. Allow Remote Connections (Optional)
To allow remote connections to PostgreSQL:
Edit the PostgreSQL configuration file:
Locate
listen_addressesand change it to:Edit the
pg_hba.conffile:Add a line like this for IPv4 connections:
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.,14or15.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
pgAdmin4 on UbuntuUpdate System Packages
Add the PostgreSQL APT Repository
Update Repositories
Install
pgAdmin4For the desktop version:For the web version:
Configure
pgAdmin4(for Web Version) If you installed the web version, configure it:Follow the prompts to set up the admin user and configuration.
Access
pgAdmin4Desktop Version: Launch it from your system's application menu.
Web Version: Open your browser and go to
http://<server-ip>/pgadmin4(usuallyhttp://localhost/pgadmin4).
Troubleshooting
If
pgAdmin4is still unavailable for your Ubuntu version, verify that your Ubuntu distribution is supported bypgAdmin. 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
tuser Table2. Insert Data into the tuser Table (Create)
tuser Table (Create)3. Retrieve Data from the tuser Table (Read)
tuser Table (Read)Retrieve All Records:
Retrieve Specific Columns:
Retrieve Data with a Condition:
4. Update Data in the tuser Table (Update)
tuser Table (Update)Update a Specific Record:
Update Multiple Fields:
5. Delete Data from the tuser Table (Delete)
tuser Table (Delete)Delete a Specific Record:
Delete All Records:
6. Drop the tuser Table
tuser TableIf 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