How to create a database in PostgreSQL
Create a PostgreSQL database with CREATE DATABASE or createdb, set its owner and encoding, copy an existing database, and handle "if not exists".
Quick Answer
Connect as a superuser or a role with CREATEDB, then run:
CREATE DATABASE shop;
To make the app's user own it, so it can create tables freely: CREATE DATABASE shop OWNER app_user;
Create a database and connect to it
postgres=# CREATE DATABASE shop;
CREATE DATABASE
postgres=# \c shop
You are now connected to database "shop" as user "postgres".
shop=# \l shop
CREATE DATABASE can't run inside a transaction block. If your migration tool wraps every file in BEGIN/COMMIT, run it separately.
In VisuaLeaf
Create and check it in VisuaLeaf's SQL editor
In VisuaLeaf's SQL editor, keep the CREATE DATABASE statement and the query that checks it in the same buffer, and run them one at a time. Select the first statement and press F9 to run only the selection:
CREATE DATABASE shop ENCODING 'UTF8' LC_COLLATE 'C' LC_CTYPE 'C' TEMPLATE template0;
SELECT datname, pg_encoding_to_char(encoding) AS encoding, datcollate, datctype
FROM pg_database
WHERE datname = 'shop';
Then select the SELECT and press F9 again. If PostgreSQL rejects the locale and encoding combination, the error appears under the first statement. If it succeeds, the result grid shows exactly what was created. Run them separately because CREATE DATABASE can't run inside a transaction block.
Click Refresh at the top of the sidebar and the new database appears in the connection tree. Save the pair with Ctrl/Cmd+S if you create databases often. See the SQL Editor docs.
Set the owner
The owner is the role that can drop the database and, from PostgreSQL 15 onward, the only non-superuser that can create tables in its public schema by default. Making the application or migration user the owner avoids most permission errors later:
CREATE DATABASE shop OWNER app_user;
-- or change it afterwards
ALTER DATABASE shop OWNER TO app_user;
Owning the database doesn't make the user owner of tables already in it. If you restore a dump created by another role, reassign the objects too, for example REASSIGN OWNED BY old_owner TO app_user; run inside that database.
Encoding, locale and template
New databases are copied from template1 and inherit its encoding and locale. To choose them explicitly, copy from template0, which has no local changes:
CREATE DATABASE shop
OWNER app_user
ENCODING 'UTF8'
LC_COLLATE 'en_US.utf8'
LC_CTYPE 'en_US.utf8'
TEMPLATE template0;
| Option | What it controls |
|---|---|
OWNER | Who owns and can drop the database |
ENCODING | Character set. Use UTF8 unless you have a reason not to. |
LC_COLLATE / LC_CTYPE | Sort order and character classification. Can't be changed later. |
TEMPLATE | Database to copy. template0 is required when changing encoding or locale. |
CONNECTION LIMIT | Max concurrent connections to this database |
Locale names differ by OS. Run SELECT collname FROM pg_collation; or locale -a on the server to see what exists.
Copy an existing database
Any database can be a template, which makes a fast local copy for testing:
CREATE DATABASE shop_copy TEMPLATE shop;
This fails with source database "shop" is being accessed by other users if anyone is still connected to shop after a short wait (see the test below). Close those sessions first (see drop database for how to find them). To copy across servers, use pg_dump and pg_restore instead.
CREATE DATABASE IF NOT EXISTS
PostgreSQL doesn't support IF NOT EXISTS on CREATE DATABASE, and because the command can't run in a transaction, a DO block won't work either. In psql, check first and run the command only if needed with \gexec:
SELECT 'CREATE DATABASE shop'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'shop')\gexec
In a shell script, check with psql -lqt or simply ignore the "already exists" error:
createdb -U postgres shop 2>/dev/null || echo "shop already exists"
What we found testing this
We ran each of these against the official postgres:16 Docker image, whose cluster locale is en_US.utf8:
TEMPLATE template0alone isn't enough to change encoding.CREATE DATABASE x ENCODING 'LATIN1' TEMPLATE template0still failed, because the locale is inherited too:
It worked once we also setERROR: encoding "LATIN1" does not match locale "en_US.utf8" DETAIL: The chosen LC_CTYPE setting requires encoding "UTF8".LC_COLLATE 'C' LC_CTYPE 'C'. Encoding and locale have to be chosen together.- A locale that isn't installed fails with a hint about ICU:
LOCALE 'de_DE.UTF-8'gaveERROR: invalid LC_COLLATE locale name: "de_DE.UTF-8"withHINT: If the locale name is specific to ICU, use ICU_LOCALE.Slim Docker images ship very few OS locales. - Anything you add to
template1appears in every new database. We created a table intemplate1, and the nextCREATE DATABASEalready contained it. That's handy for extensions you always want, and the reason to usetemplate0when you need a clean copy. - A busy template makes CREATE DATABASE wait about 5 seconds before failing. With one session connected to the source, it took 5.08 s to return
source database "shop" is being accessed by other userswithDETAIL: There is 1 other session using the database.The same happened withSTRATEGY file_copy. The wait gives sessions a chance to leave, so a copy can succeed if the last client disconnects in time. - The exact errors behind two common questions: inside
BEGINyou getERROR: CREATE DATABASE cannot run inside a transaction block, andIF NOT EXISTSgivessyntax error at or near "NOT".
From the command line: createdb
createdb ships with PostgreSQL and runs CREATE DATABASE for you:
createdb -U postgres shop
createdb -U postgres -O app_user -E UTF8 -T template0 shop
Add -e to see the statement it sends: CREATE DATABASE shop OWNER app_user ENCODING 'UTF8'; On Linux installs where only the postgres OS user can connect: sudo -u postgres createdb shop.
Frequently Asked Questions
Does PostgreSQL support CREATE DATABASE IF NOT EXISTS?
No. In psql, use SELECT 'CREATE DATABASE x' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'x')\gexec, or ignore the "already exists" error in a script.
Why do I get "permission denied to create database"?
Your role lacks the CREATEDB attribute. A superuser can grant it with ALTER ROLE your_user CREATEDB.
Why does CREATE DATABASE fail with "source database template1 is being accessed by other users"?
Someone is connected to template1, which every new database copies by default. Disconnect that session, or create the database from template0.
How do I list databases in PostgreSQL?
Run \l in psql, or SELECT datname FROM pg_database from any client.