QRG Setup
Quick Reference Guide: Setting Up PostgreSQL on Ubuntu
1. Install
Update System Packages
Make sure your system packages are up-to-date:
sudo apt update && sudo apt upgrade -y
Install PostgreSQL
Install PostgreSQL and its contrib package:
sudo apt install -y postgresql postgresql-contrib
2. Start and Ensure It's Running
Initialize the Database Cluster
Initialization occurs automatically during installation. To manually reinitialize:
sudo -u postgres /usr/bin/initdb /var/lib/postgresql/data
Start and Enable the PostgreSQL Service
Ensure PostgreSQL starts at boot and is currently running:
sudo systemctl enable postgresql
sudo systemctl start postgresql
Verify the service status:
sudo systemctl status postgresql
3. Create a User and Database
Access the PostgreSQL Shell
Switch to the postgres
user and open the PostgreSQL shell:
sudo -i -u postgres
psql
Create a Database and User
Inside the PostgreSQL shell:
CREATE DATABASE my_database;
CREATE USER my_user WITH PASSWORD 'my_password';
GRANT ALL PRIVILEGES ON DATABASE my_database TO my_user;
- Note: Forgetting semicolons is a common mistake, you'll see no output and then when you check for the object you will find that its not there
Checking Common Object Types
\l #List databases
\du #List Users
Exit the shell:
\q