Guide
How to create a database in PostgreSQL
Connect with psql as a superuser or a role with CREATEDB and run CREATE DATABASE appdb OWNER app_owner;, then \c appdb to switch to it. From a shell, createdb -O app_owner appdb does the same. Check it exists with \l. Use TEMPLATE template0 when you need an encoding or locale different from the server default.
CREATE DATABASE in psql
$ psql -h db.example.com -U postgres -d postgres
postgres=# CREATE DATABASE appdb;
CREATE DATABASE
postgres=# \c appdb
You are now connected to database "appdb" as user "postgres".You need a connection to some existing database to run the command, usually the default postgres database. The new database is a copy of template1, so anything you have installed there (extensions, tables) comes along.
Two rules that catch people out. CREATE DATABASE can't run inside a transaction block, so a client that wraps everything in BEGIN gets CREATE DATABASE cannot run inside a transaction block. And there is no IF NOT EXISTS. In psql, \gexec gives you one: it runs the query's result as SQL, and the query returns nothing when the database already exists.
SELECT 'CREATE DATABASE appdb OWNER app_owner'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'appdb')\gexecCreate the owner first
Don't leave application databases owned by postgres. Create a role for the application and make it the owner, so it can create tables in the database without being a superuser:
CREATE ROLE app_owner LOGIN PASSWORD 'use-a-long-random-password';
CREATE DATABASE appdb OWNER app_owner;
-- optional: only roles you grant CONNECT to can log in
REVOKE CONNECT ON DATABASE appdb FROM PUBLIC;
GRANT CONNECT ON DATABASE appdb TO app_owner;To give a database to another role you must be able to act as that role. Since PostgreSQL 16 the error is must be able to SET ROLE "app_owner". A superuser always can; anyone else needs membership in that role, for example GRANT app_owner TO alice;. Since PostgreSQL 15, the public schema of a new database belongs to the database owner and other users can't create objects in it unless you grant CREATE. More on roles, passwords and grants in how to create a user in PostgreSQL.
To hand over an existing database: ALTER DATABASE appdb OWNER TO app_owner;. That changes the database's owner, not the owner of the tables inside it.
Encoding, locale and templates
A new database takes its encoding and locale from template1, usually UTF8 and whatever locale the server was initialised with. To choose different ones, copy template0, the pristine template, because PostgreSQL won't re-sort indexes that might already exist in template1:
CREATE DATABASE appdb
OWNER app_owner
ENCODING 'UTF8'
LOCALE 'en_US.UTF-8'
TEMPLATE template0;
-- ICU collation (if the server was built with ICU)
CREATE DATABASE appdb_icu
LOCALE_PROVIDER icu ICU_LOCALE 'en-US'
TEMPLATE template0;
-- PostgreSQL 17+: the builtin provider, the same on every platform
CREATE DATABASE appdb_c
LOCALE_PROVIDER builtin BUILTIN_LOCALE 'C.UTF-8'
TEMPLATE template0;Locale names come from the operating system, so en_US.UTF-8 may be spelled en_US.utf8 on Linux; locale -a on the server lists them. Pick the locale carefully: it decides how ORDER BY sorts text and is hard to change once the database holds data.
Other options worth knowing: CONNECTION LIMIT 50 caps concurrent connections, IS_TEMPLATE true lets others clone the database, and STRATEGY chooses how it is copied (WAL_LOG, the default since PostgreSQL 15, or FILE_COPY).
createdb from the command line
createdb ships with psql and is a thin wrapper around CREATE DATABASE. It takes the usual connection flags:
createdb -h db.example.com -U postgres -O app_owner appdb
createdb -h db.example.com -U postgres -E UTF8 -l en_US.UTF-8 -T template0 appdb
createdb --echo appdb "Orders service" # prints the SQL it runs; the last argument becomes a commentThe opposite is dropdb. Both connect to the postgres database by default; pass --maintenance-db if you can't connect there.
Who can create databases
A superuser, or a role with the CREATEDB attribute. Without it you get permission denied to create database. A superuser grants it with:
ALTER ROLE alice CREATEDB;Amazon RDS and Aurora. There is no true superuser. The master user you set when creating the instance is a member of rds_superuser and has CREATEDB and CREATEROLE, so CREATE DATABASE works as above. If you didn't name a database at creation, connect to postgres. See connecting to RDS PostgreSQL.
Supabase. Each project comes with one database, postgres, and that is where you normally work: put your tables in schemas there. You can run CREATE DATABASE and reach the new database with psql or other clients, but Supabase's own services (the auto-generated API, Auth, Storage) and the dashboard only use postgres. For a separate database with all of those, Supabase suggests a new project.
Neon. New projects get a database called neondb. Add more in the console (Databases, then Add database), with the CLI (neon databases create --name mydb --owner-name alice), through the API, or with CREATE DATABASE. Databases created in the console, CLI or API also grant all privileges to neon_superuser; ones created in SQL don't, so grant access yourself. Neon allows 500 databases per branch.
List databases
\l -- in psql
psql -l # from the shell
SELECT datname,
pg_get_userbyid(datdba) AS owner,
pg_encoding_to_char(encoding) AS encoding,
datcollate,
pg_size_pretty(pg_database_size(datname)) AS size
FROM pg_database
WHERE NOT datistemplate
ORDER BY datname;The SQL version works in any client. More listing commands are in the psql commands cheat sheet.
Drop a database
DROP DATABASE appdb;
DROP DATABASE IF EXISTS appdb;
DROP DATABASE appdb WITH (FORCE); -- PostgreSQL 13+: disconnects other sessions first
dropdb -h db.example.com -U postgres appdb
dropdb --if-exists --force appdbOnly the owner or a superuser can drop a database (must be owner of database appdb), and not while you are connected to it (cannot drop the currently open database): connect to postgres first. Dropping removes everything in it and can't be undone, so take a backup with pg_dump if there is any doubt.
Without FORCE, PostgreSQL waits a few seconds for other sessions to leave and then fails with database "appdb" is being accessed by other users. WITH (FORCE) terminates them, but it still fails if the database has prepared transactions, active logical replication slots or subscriptions.
Fix common errors
database "appdb" already exists. Pick another name, drop the old one, or use the \gexec pattern above in scripts.
permission denied to create database. Your role lacks CREATEDB. Ask a superuser (on RDS, the master user) to run ALTER ROLE you CREATEDB; or to create it for you.
must be able to SET ROLE "app_owner" (PostgreSQL 16+; older versions say must be member of role). You tried OWNER app_owner for a role you can't act as. Create the database as a superuser, or GRANT app_owner TO you; first.
source database "template1" is being accessed by other users. Some session is connected to template1, often a GUI that opened it by default. Find it and close it:
SELECT pid, usename, application_name, client_addr
FROM pg_stat_activity WHERE datname = 'template1';
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'template1';new encoding (LATIN1) is incompatible with the encoding of the template database (UTF8). Add TEMPLATE template0, as the hint says.
encoding "LATIN1" does not match locale "en_US.UTF-8". The encoding and locale must agree; choose a matching locale, or LOCALE 'C', which works with any encoding.
Questions
What is the difference between CREATE DATABASE and createdb?
None in effect: createdb is a command-line program that connects to the server and runs CREATE DATABASE for you. Use whichever fits; createdb --echo shows the SQL it sends.
Does PostgreSQL support CREATE DATABASE IF NOT EXISTS?
No. In psql, run SELECT 'CREATE DATABASE appdb' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'appdb')\gexec, or check pg_database in your script first.
How do I create a database and a user for it?
Create the role first with CREATE ROLE app_owner LOGIN PASSWORD '...', then CREATE DATABASE appdb OWNER app_owner. The owner can then create schemas and tables in it.
How do I switch to the new database in psql?
Type \c appdb. PostgreSQL has no USE statement; each connection belongs to one database, so \c opens a new connection.
How do I drop a database that has active connections?
On PostgreSQL 13 or later, run DROP DATABASE appdb WITH (FORCE); or dropdb --force appdb. On older versions, terminate the sessions with pg_terminate_backend() first.
Updated 2026-11-25