The Quick Answer
Open psql as the postgres superuser and create the database:
sudo -u postgres psql
CREATE DATABASE appdb;PostgreSQL confirms with CREATE DATABASE. Leave psql with \q. The same from the shell, without opening psql:
sudo -u postgres createdb appdbUnquoted names are folded to lowercase. Use lowercase letters, digits, and underscores, and you never need double quotes around the name.
Create It with an Owner
An application should not connect as postgres. Create a login role for it first (the details are in PostgreSQL create user), then make that role the owner:
CREATE ROLE app_user WITH LOGIN PASSWORD 'STRONG_PASSWORD';
CREATE DATABASE appdb OWNER app_user;With createdb, the owner is set with -O:
sudo -u postgres createdb -O app_user appdbTo change the owner later, run ALTER DATABASE appdb OWNER TO app_.
Encoding and Locale
A new database is a copy of template1 and takes its encoding and locale. To choose different ones, copy the clean template0 instead:
CREATE DATABASE appdb
OWNER app_user
ENCODING 'UTF8'
LC_COLLATE 'en_US.UTF-8'
LC_CTYPE 'en_US.UTF-8'
TEMPLATE template0;The locale must exist on the server; locale -a lists the installed ones. The collation decides sort order and cannot be changed in place later, so settle it before the application stores data.
Give the Application Access
- The role owns the database: the simplest choice for one application. The owner can connect and, on PostgreSQL 15 and later, create tables in the
publicschema. - Another owner, plus grants: useful when several roles share one database, or when a migration role differs from the role the application runs as.
For the second pattern, grant the rights while connected to the database itself, because a schema belongs to one database:
GRANT CONNECT ON DATABASE appdb TO app_user;
\c appdb
GRANT USAGE, CREATE ON SCHEMA public TO app_user;The public Schema Since PostgreSQL 15
Up to PostgreSQL 14, every role could create objects in the public schema of any database it could connect to. PostgreSQL 15 removed that right for ordinary roles in new clusters and newly created databases. The schema is now owned by pg_, so whoever owns the database keeps it. A cluster upgraded from an older version, or a database restored from an older dump, keeps its old permissions. If an application that worked on an older server now fails with a "permission denied for schema public" error, make its role the database owner or add the schema grant shown above.
Connect and List Databases
psql -h localhost -U app_user -d appdb
\l
\c appdb\l lists databases with their owner and encoding, and \c switches to another one. More ways to list them are in PostgreSQL list databases, and the everyday meta-commands are in the psql commands cheat sheet.
Drop a Database
Dropping removes every table and row at once and cannot be undone. Take a dump first with pg_
DROP DATABASE appdb;You cannot drop the database you are connected to, so connect to postgres first. While other sessions are open, PostgreSQL refuses: stop the application, then retry. PostgreSQL 13 and later also accept DROP DATABASE appdb WITH (FORCE);, which ends those sessions (it still fails if prepared transactions or logical replication slots use the database); use it only after the application is stopped. The shell equivalent is dropdb.
Permission Denied to Create Database
This error means the role you logged in with is not a superuser and lacks the CREATEDB attribute. Either create the database as postgres, as shown above, or let the role create its own:
ALTER ROLE app_user CREATEDB;Give that right only to roles that need it, such as a role used by a test suite. A role an application runs as rarely does.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations