Installing PostgreSQL
Introduction
PostgreSQL is a powerful, open-source object-relational database system. It has a strong reputation for reliability, feature robustness, and performance. This tutorial will guide you through the process of installing PostgreSQL on a Linux system.
Prerequisites
Before you begin, ensure you have the following:
- A running instance of a Linux-based OS (e.g., Ubuntu, CentOS).
- Sudo or root access to install packages.
- Basic knowledge of the terminal and command-line operations.
Updating the Package List
It's a good practice to update the package list on your Linux system before installing a new software package. Run the following command:
sudo apt update
This command fetches the latest package information from the repositories configured on your system.
Installing PostgreSQL
To install PostgreSQL, use the package manager for your Linux distribution. For Ubuntu/Debian-based systems, use the following command:
sudo apt install postgresql postgresql-contrib
This command installs both the PostgreSQL core database server and additional utilities.
Starting PostgreSQL Service
Once the installation is complete, you need to start the PostgreSQL service. Use the following command:
sudo systemctl start postgresql
To enable PostgreSQL to start on boot, use:
sudo systemctl enable postgresql
Checking PostgreSQL Status
To verify that PostgreSQL is running, check its status with the command:
sudo systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Mon 2021-02-15 12:34:56 UTC; 1min 23s ago
...
If PostgreSQL is running, you should see an active (exited) status.
Accessing PostgreSQL
PostgreSQL creates a default user named postgres
. To access the PostgreSQL prompt, switch to the postgres
user and open the PostgreSQL command line interface (CLI) using:
sudo -i -u postgres
psql
You will see the PostgreSQL prompt:
postgres=#
Creating a New Database and User
To create a new database, run the following SQL command at the PostgreSQL prompt:
CREATE DATABASE mydb;
To create a new user with a password, use:
CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword';
Grant all privileges on the database to the new user:
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Conclusion
Congratulations! You have successfully installed PostgreSQL on your Linux system. You also learned how to start the PostgreSQL service, check its status, and create a new database and user. You are now ready to start using PostgreSQL for your projects.