Ref
- https://g.co/gemini/share/d10d3ed58283
The easiest way to install the PostgreSQL database on Ubuntu 22.04 is by using the default Ubuntu repository, which is an extremely straightforward process.
Here are the step-by-step instructions:
1. Update Package Index
First, update your local package list to ensure you have access to the latest versions:
sudo apt update
2. Install PostgreSQL2
Install the main PostgreSQL server package and the postgresql-contrib package, which adds useful extra utilities and extensions:
sudo apt install postgresql postgresql-contrib -y
This command will install the latest version of PostgreSQL available in the Ubuntu 22.04 repository (typically version 14 or a later update). The installation process automatically starts the PostgreSQL service and sets it to start on boot.
3. Verify Installation and Service Status
You can check if the service is running correctly:
sudo systemctl status postgresql
The output should show Active: active (exited) or Active: active (running), confirming the service is active.
4. Connect to PostgreSQL4
By default, the installation creates a Unix user named postgres which is associated with the default PostgreSQL administrative role (also called postgres). You can log in as this user to manage the database.
To access the PostgreSQL prompt (psql):
Switch to the
postgressystem user:Bashsudo -i -u postgresAccess the PostgreSQL prompt:
Bashpsql
You will now be at the PostgreSQL prompt, indicated by postgres=#.
postgres=#
To exit the PostgreSQL prompt, type:
\q
Then, type exit to return to your regular Ubuntu user prompt.
5. Basic Post-Installation Setup (Optional but Recommended)
For better security and usability, you should set a password for the default postgres database user.
Connect to the PostgreSQL prompt as the
postgresuser:Bashsudo -u postgres psqlSet a new password:
Replace 'your_strong_password' with a secure password.
SQLALTER USER postgres WITH PASSWORD 'your_strong_password';Exit the prompt:
SQL\q
You can now use this password for remote connections or when configuring client applications.