Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

rails db:create stops with Couldn't create database

ErrorCouldn'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.

  • Applies to Rails 7.2, Rails 8.0, PostgreSQL 16, PostgreSQL 17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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:create
  2. 2Match what you see, then jump to its fix

    1. 1The line after the Rails message says permission denied for creating a database.The database role is not allowed to create databasesFix
    2. 2The 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. 3The PostgreSQL line mentions encoding or locale of the template database.The new database does not fit the template it is copied fromFix

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:create

Confirmed by postgresql.org

Did this fix it?

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.yml

2Fix

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_URL

3Verify

Run the task again and confirm the connection goes to the intended server.

bin/rails db:create

Confirmed by guides.rubyonrails.org

Did this fix it?

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 -l

2Fix

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:prepare

3Verify

db:prepare loads the schema and finishes without errors.

bin/rails db:prepare

Confirmed by postgresql.org

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.