rails db:create stops with Couldn't create database
Couldn't create database for {"adapter"=>"postgresql"Short answer
rails db:create asks PostgreSQL to run CREATE DATABASE, so the connection role needs the CREATEDB attribute or superuser rights. The command also fails when database.yml or DATABASE_URL points at the wrong host or user, or when the encoding clashes with the template database. Read the line after the Rails message.
Fix it fast
1Find out which cause you have
Rails prints the real PostgreSQL error right after its own message. Run the task again and read that line, then pick the cause below.
bin/rails db:create2Match what you see, then jump to its fix
- 1
The line after the Rails message says permission denied for creating a database.The database role is not allowed to create databasesFix - 2
The error names a host, user or socket you did not expect, or a login failure.database.yml and DATABASE_URL disagree, so Rails connects somewhere elseFix - 3
The PostgreSQL line mentions encoding or locale of the template database.The new database does not fit the template it is copied fromFix
- 1
What does the error look like?
Couldn't create database for {"adapter"=>"postgresql"
Cause 1 of 3
The database role is not allowed to create databases
1Check
CREATE DATABASE needs a superuser or a role with the CREATEDB privilege. A role made with plain CREATE ROLE has NOCREATEDB unless you say otherwise.
psql -X -c '\du'2Fix
Give the role named in database.yml the CREATEDB attribute (run as a superuser).
ALTER ROLE myapp CREATEDB;3Verify
Run the Rails task again.
bin/rails db:createConfirmed by postgresql.org
Cause 2 of 3
database.yml and DATABASE_URL disagree, so Rails connects somewhere else
1Check
When both exist, Rails merges them and the environment variable wins on any conflict. An explicit url: key makes Rails ignore DATABASE_URL. A leftover DATABASE_URL in your shell can override the file.
echo "$DATABASE_URL"
grep -n -A8 '^development' config/database.yml2Fix
Use one source. Unset the variable, or put the intended values in database.yml. Without a host, libpq connects through the local Unix socket, and the user defaults to your operating system user name.
unset DATABASE_URL3Verify
Run the task again and confirm the connection goes to the intended server.
bin/rails db:createConfirmed by guides.rubyonrails.org
Cause 3 of 3
The new database does not fit the template it is copied from
1Check
A new database copies template1 by default, and its encoding and locale must match that template unless template0 is used. Rails asks for unicode encoding in database.yml.
psql -X -l2Fix
Create the database yourself from template0 (it carries no template encoding to clash with), then let Rails load the schema.
createdb -T template0 myapp_development
bin/rails db:prepare3Verify
db:prepare loads the schema and finishes without errors.
bin/rails db:prepareConfirmed by postgresql.org
How do I stop it coming back?
- Create the application role with CREATEDB in development, but not in production.
- Keep connection settings in one place: DATABASE_URL or database.yml, not both.
- Use db:prepare in setup scripts so reruns do not fail on an existing database.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
psql says the role does not exist, even for postgres or your own user
FATAL: role "username" does not exist
The server found no role with the name the client sent.
3 causes, each with a fixDatabases
Cannot connect to PostgreSQL on port 5432 from another machine
FATAL: no pg_hba.conf entry for host "198.51.100.7", user "app", database "appdb"
Two settings guard remote access.
3 causes, each with a fixBack end
Go database/sql fails to scan a NULL column, and stores empty strings instead of NULL
unsupported Scan, storing driver.Value type <nil> into type *string
database/sql hands a NULL to Scan as nil, and a plain *string cannot hold it, so Scan returns an error.
2 causes, each with a fixBack end
Hibernate: PersistentObjectException, detached entity passed to persist
jakarta.persistence.EntityExistsException
persist() is for new objects.
2 causes, each with a fixBack end
Knex runs out of connections and the query waits until it times out
Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
Every query needs a free connection from Knex's pool.
3 causes, each with a fixBack end
Node.js cannot reach PostgreSQL: connect ETIMEDOUT in production
at TCPConnectWrap.afterConnect [as oncomplete]
ETIMEDOUT means the other side never answered the connection attempt.
3 causes, each with a fix