PostgreSQL Setup & Database Creation – Guide
Get PostgreSQL up and running, create a user, database, and get hacking!
Step 1: Install PostgreSQL
Windows:
- Download the installer: https://www.postgresql.org/download/windows/
- Use the Stack Builder to install additional tools like pgAdmin
- Remember your password during install
Linux (Debian/Ubuntu):
bash
sudo apt update
sudo apt install postgresql postgresql-contribmacOS (with Homebrew):
bash
brew install postgresql
brew services start postgresqlStep 2: Check PostgreSQL Status (Linux/macOS)
bash
sudo systemctl status postgresqlStep 3: Switch to the Default 'postgres' User
bash
sudo -i -u postgresNow access the PostgreSQL shell:
bash
psqlYou’re in!
Step 4: Create a New Role (User)
Inside the psql prompt:
sql
CREATE ROLE myuser WITH LOGIN PASSWORD 'mypassword';Give it permission to create DBs:
sql
ALTER ROLE myuser CREATEDB;Exit:
sql
\qStep 5: Create a Database for the User
Switch to your OS user shell and run:
bash
createdb -U myuser mydatabaseOr, inside psql:
sql
CREATE DATABASE mydatabase OWNER myuser;Step 6: Connect to the DB
bash
psql -U myuser -d mydatabaseIt may ask for your password. Type
mypassword.
Bonus Commands
- List all users:
sql
\du- List all databases:
sql
\l- Connect to a DB:
sql
\c mydatabase- Show tables:
sql
\dt- Exit:
sql
\qCleanup (Optional)
- Drop database:
sql
DROP DATABASE mydatabase;- Drop user:
sql
DROP ROLE myuser;You're now PostgreSQL-enabled!
Secure. Reliable. SQL warrior mode: activated. ⚔️