How to create users and databases in PostgreSQL
Updated 7/20/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 secure MongoDB with authentication
By default, MongoDB runs without a password. Here is how to enable authentication and create an admin user.
Updated 7/20/2026DatabasesHow to migrate a database between servers
Move a MySQL or PostgreSQL database to a new server with minimal downtime, using dump and restore.
Updated 7/20/2026DatabasesHow to connect an app to a remote database
Allow access to a database from another server, securely: bind address, dedicated user and firewall.
Updated 7/20/2026