DevForth

Install PostgreSQL and create a database and user from the command line

Install PostgreSQL on Ubuntu or WSL, then create a database owned by its own user in two SQL commands, and connect with one URL.

Ivan Borshchov
Ivan Borshchov
CEO & System Architect @ DevForth
Install PostgreSQL and create a database and user from the command line
Install PostgreSQL and create a database and user from the command line

PostgreSQL is a database: a very tidy cupboard where your app keeps its data. Here is the shortest way to get one on Ubuntu (or Ubuntu inside WSL on Windows) and give your app its own shelf.

Install it

sudo apt update
sudo apt install postgresql

Ubuntu starts the service for you. On WSL without systemd, start it with sudo service postgresql start.

Open the database console

Postgres creates a system user called postgres, which is the boss of the database. Borrow it to open psql:

sudo -u postgres psql

This step needs no extra password.

Create a user and a database

CREATE USER myapp WITH PASSWORD 'change-me';
CREATE DATABASE myapp OWNER myapp;

The important part is making the user the owner. Older guides run GRANT ALL PRIVILEGES ON DATABASE ... followed by a list of grants on tables and sequences. Since PostgreSQL 15 that is no longer enough to create tables in the public schema, and the app fails with "permission denied". An owner can do anything inside its own database, which is usually exactly what an app needs.

Type \q to leave.

Check that it works

psql "postgresql://myapp:change-me@localhost:5432/myapp" -c "select 1"

Most frameworks and ORMs expect this same string in DATABASE_URL.

Two small safety habits

  • Use a long random password instead of change-me.
  • Keep Postgres listening on localhost, which is the default. If another server has to connect, open the port to that server only.

In short: install it, open sudo -u postgres psql, create a user, create a database owned by that user, and connect with one URL.

Get your project quote

Free result within 48-72 hours

Related insights