SurferCloud Blog SurferCloud Blog
  • HOME
  • NEWS
    • Latest Events
    • Product Updates
    • Service announcement
  • TUTORIAL
  • COMPARISONS
  • INDUSTRY INFORMATION
  • Telegram Group
SurferCloud Blog SurferCloud Blog
SurferCloud Blog SurferCloud Blog
  • HOME
  • NEWS
    • Latest Events
    • Product Updates
    • Service announcement
  • TUTORIAL
  • COMPARISONS
  • INDUSTRY INFORMATION
  • Telegram Group
  • banner shape
  • banner shape
  • banner shape
  • banner shape
  • plus icon
  • plus icon

How to Install PostgreSQL on Ubuntu: Step-by-Step Guide

December 13, 2024
3 minutes
TUTORIAL
392 Views

PostgreSQL is a powerful, open-source relational database management system known for its reliability, flexibility, and extensive feature set. This guide provides a clear, step-by-step process for installing PostgreSQL on Ubuntu.


Step 1: Update System Packages

Before installing PostgreSQL, it’s good practice to update the package list to ensure you have access to the latest versions. Run the following commands:

sudo apt-get update
sudo apt-get upgrade -y

These commands update your system’s package index and install any available updates.


Step 2: Install PostgreSQL

Ubuntu includes PostgreSQL in its default repositories, so installation is simple. Run the following command to install PostgreSQL along with some related utilities:

sudo apt-get install -y postgresql postgresql-contrib
  • postgresql: Installs the core PostgreSQL database package.
  • postgresql-contrib: Installs useful additional tools and extensions.

Once the process is complete, PostgreSQL will be installed on your system.


Step 3: Start and Enable PostgreSQL

After installation, you’ll want to ensure that PostgreSQL is running and set to start on boot. Use these commands:

sudo systemctl start postgresql
sudo systemctl enable postgresql
  • start: Starts the PostgreSQL service.
  • enable: Configures PostgreSQL to start automatically when the system boots.

Step 4: Verify PostgreSQL Installation

To check if PostgreSQL is running, use the following command:

sudo systemctl status postgresql

If PostgreSQL is running correctly, you’ll see an "active (running)" status.


Step 5: Access PostgreSQL Shell

To interact with PostgreSQL, you’ll need to switch to the PostgreSQL user account. Do this with the following command:

sudo -i -u postgres

Once you’re in the PostgreSQL user environment, enter the PostgreSQL command-line interface (CLI) using:

psql

You should see the postgres=# prompt, indicating that you are now inside the PostgreSQL shell.


Step 6: Create a New Database and User

To create a new database, run the following SQL command:

CREATE DATABASE mydatabase;

To create a new user with a password, run:

CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword';

Grant the user access to the new database:

GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;

Exit the PostgreSQL shell by typing:

\q

Then, exit the PostgreSQL user environment by typing:

exit

Step 7: Connect to PostgreSQL

To connect to the PostgreSQL database from your main system user account, use this command:

psql -U myuser -d mydatabase -h 127.0.0.1 -p 5432
  • -U: Specifies the PostgreSQL user.
  • -d: Specifies the database name.
  • -h: Specifies the host (local machine in this case).
  • -p: Specifies the port (default PostgreSQL port is 5432).

Why Choose SurferCloud?

Need a reliable VPS to host your PostgreSQL database? SurferCloud offers fast, secure, and globally distributed servers with high uptime. With data centers across Asia, Europe, and the Americas, SurferCloud ensures low latency and fast performance for your PostgreSQL applications. It’s an affordable, flexible, and scalable choice for developers and businesses.

Tags : install PostgreSQL server install PostgreSQL Ubuntu PostgreSQL cloud hosting PostgreSQL database Ubuntu PostgreSQL on VPS Ubuntu PostgreSQL setup VPS for PostgreSQL

Related Post

4 minutes TUTORIAL

How to Install and Use PNPM on Linux?

PNPM (Performant NPM) is a fast, disk space-efficient p...

4 minutes TUTORIAL

How to Mount a Disk on SurferCloud: A Step-by

Mounting a disk is an essential task when setting up cl...

5 minutes TUTORIAL

How to Check CPU Usage in Linux: A Comprehens

Monitoring CPU usage is crucial for maintaining system ...

Affordable CDN

ucdn

2025 Special Offers:

annual vps

Light Server promotion:

ulhost-promo

Cloud Server promotion:

cloud server

Copyright © 2024 SurferCloud All Rights Reserved.  Sitemap.