How to Install PostgreSQL and Set It Up on a Linux Server
PostgreSQL is a powerful, open-source relational database system used by developers, startups, and large enterprises worldwide.
In this guide, I will walk you through installing PostgreSQL on a Linux server and doing a basic setup so you can start using it right away.
Prerequisites
Before you begin, make sure you have:
- A Linux server (Ubuntu/Debian or CentOS/RHEL)
- sudo or root access
- An internet connection
- Terminal(I used git bash)
๐ Step 1: Access Your Linux Server
First, connect to your Linux server via SSH:
ssh username@server_ip_address
1.Replace the username with server's user(for example in my case mawia)
2.Replace the server_ip_address with your server's ip address
You can as well choose to specify the port after inputing the above( -p 22) but its not a must
๐ Step 2: View Files and Directories
Once connected, you can check the contents of your home directory:
This can be done by;
ls
command on the terminal you are using
This helps confirm youโre in the right place before proceeding.
๐ Step 3: Update the System
Itโs best to ensure your system is up-to-date before installing software to avoid vulnerabilities and compatibility issues.
If using Ubuntu, you can use the command;
sudo apt update
๐ Step 4: Install PostgreSQL
The command used in ubuntu is;
sudo apt install postgresql postgresql-contrib -y
After inputing this command you will be asked to choose yes or no please choose yes so as to continue with the process of installation as shown below;
๐ Step 5: Check whether you have PostgreSQL /start
You use the command;
sudo systemctl start PostgreSQL
For example, from the above the state is active
๐ Step 6:Check the version of PostgreSQL
You use the command;
psql --version
For example my version was 16.9 from the screenshot I took below.
๐ Step 7:Now Enable the PostgreSQL
use the command;
sudo systemctl enable postresql
As show below, the PostgreSQL is now active.
๐ Step 8:Check Status
use the command;
sudo systemctl status postgesql
Expect something the this;
๐ Step 9: Now its time to interact with the PostgreSQL
Now that PostgreSQL is installed and running, letโs log in and create our first database.
๐ Switching to PostgreSQL default user
PostgreSQL creates a default Linux user called postgres
. Switch to it:
we use the command;
sudo -i -u postresql
- i- stands for interface
- u- user
- postgresql is the default Postresql user
You should see your prompt change to:
postgres@myvm:~$
๐ Access the PostgreSQL Shell
Sinceyouโre the postgres user, open the PostgreSQL interactive terminal using the command;
psql
Example of the result to ecpect is shown below;
๐ Now list the databases created
Using the command,
\l
The default database is postgres as shown below;
๐ Create a new database
Inside the psql shell, now create a new database
using the command;
create database nameofdatabase;
Remember to terminate the query
for example in my case I was creating a database named student.
so i used the command;
create database student
๐ create user
Now create user, with the command;
create user nameofuser with password 'passwordofchoice'
eg in my case;
create user lux with password '1234';
Remember to terminate the query
๐ Grant user access to the database user
using the command;
GRANT ALL PRIVILIGES ON DATABASE nameofdatabase TO username
for example in my case,
GRANT ALL PRIVILEGES ON DATABASE STUDENT TO LUX
๐ Now check the databases created
This can be done by using the command;
\l
yow will see the default one which is postgres and the one you just created in my case it was student.
WRAPPING UP!
Congratulations!!!!!!!!!!!!!!!
You have successfully installed and set up PostgreSQL on ubuntu, created your first database, and learned how to view all the available databases with the \l
command.
From here, you can connect PostgreSQL to your favorite tool and applications. For example, you might use a postgreSQL GUI tool like pgDdmin or DBeaver, or connect directly from a programming language like python,Node.js or Java.
Here is the general format for a PostgreSQL connecting string:
postgesql://username:password@host:5432/databasename
from the above replace username
,password
host
and databasename
with your own values.
Exiting PostgreSQL and the Terminal
When you're done:
- leave the PostgreSQL shell by typing the command;
\q
- If you are connected to your ubuntu server via SSH, log out by typing the command;
exit
--- That's it!!!! You are ready now to start building applications backed by a powerful PostgreSQL database. Whether you're developing locally or deploying to production, this setup will give you a reliable foundation for storing and managing your data.
**
Top comments (0)