How to create users and databases in PostgreSQL
Updated 11/29/2026
In PostgreSQL, users are called "roles". Here is how to create a role, a database and link them.
1. Enter the console
sudo -u postgres psql
2. Create a role with a password
CREATE ROLE app_user WITH LOGIN PASSWORD 'strong_password';
3. Create the database
CREATE DATABASE app_db OWNER app_user;
4. Grant privileges
GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;
5. Connect as the new user
psql -U app_user -d app_db -h 127.0.0.1
Tips
- A role can have login rights or not; use
WITH LOGINfor application accounts. - For fine table privileges, use
GRANT SELECT, INSERT ... ON table TO app_user;. - For remote access, see connect to a remote database.
Put this guide into practice on your own VPS
EU cloud servers, billed hourly, ready in minutes, with one-click installs for dozens of apps.
See pricingRelated tutorials
Databases
How to install and secure PostgreSQL on Ubuntu
Install the most advanced open-source database and configure it safely: password, scram-sha-256 authentication and controlled access.
Updated 11/29/2026DatabasesHow to install and secure MySQL on Ubuntu
Install MySQL or MariaDB on your VPS and secure it properly: root password, removing test accounts and controlled access.
Updated 11/29/2026DatabasesHow to create a MySQL user and grant privileges
Create dedicated MySQL users per application and grant exactly the privileges needed, step by step.
Updated 11/29/2026