Skip to main content

Command Palette

Search for a command to run...

Step-by-Step Guide: Create PostgreSQL Database, User, and Grant Privileges on Ubuntu

Updated
2 min read
Step-by-Step Guide: Create PostgreSQL Database, User, and Grant Privileges on Ubuntu
M

I am Mandeep Singh, a full-stack developer. I created this Blog to bestow my coding experience and love to write on JavaScript, Next.js, Laravel and little bit Unity Game development.

Here is the correct way to create a PostgreSQL database, user, and give full permissions on Ubuntu.

1️⃣ Login to PostgreSQL

sudo -i -u postgres
psql

2️⃣ Create Database

CREATE DATABASE "the-ai-shift";

3️⃣ Create User

Example user: ai_user
Example password: StrongPassword123

CREATE USER ai_user WITH PASSWORD 'StrongPassword123';

4️⃣ Give Full Permissions to Database

GRANT ALL PRIVILEGES ON DATABASE "the-ai-shift" TO ai_user;

5️⃣ Connect to Database

\c "the-ai-shift"

6️⃣ Give Full Permissions on Schema (Important)

Without this Laravel migrations may fail.

GRANT ALL ON SCHEMA public TO ai_user;

Optional (recommended):

ALTER SCHEMA public OWNER TO ai_user;

7️⃣ Verify

List databases:

\l

List users:

\du

8️⃣ Laravel .env Example

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=the-ai-shift
DB_USERNAME=ai_user
DB_PASSWORD=StrongPassword123

9️⃣ Test Connection

psql -U ai_user -d "the-ai-shift" -h localhost

One-line quick version (fast way):

CREATE DATABASE "the-ai-shift";
CREATE USER ai_user WITH PASSWORD 'StrongPassword123';
GRANT ALL PRIVILEGES ON DATABASE "the-ai-shift" TO ai_user;
ALTER DATABASE "the-ai-shift" OWNER TO ai_user;

💡 Pro tip (Laravel + PostgreSQL developers usually miss this):

If migrations fail with permission error, run:

ALTER SCHEMA public OWNER TO ai_user;