slhck
slhck

Reputation: 38672

password authentication failed for user "root" when dumping Rails schema as SQL

I am using Rails 5.2.3 with PostGIS 2.5 running on Postgres 11, set up in a Docker Compose environment. I've set the following in config/application.rb:

config.active_record.schema_format = :sql

So that special features in the Schema can get dumped. However, doing so during db:migrate, I get the following error:

pg_dump: [archiver (db)] connection to database "development" failed: FATAL: password authentication failed for user "root"

The command that gets executed is:

pg_dump -s -x -O -f /var/www/dev/db/structure.sql -T geography_columns -T geometry_columns -T layer -T raster_columns -T raster_overviews -T spatial_ref_sys -T topology development

The reason for the error is that there is no root user here; in fact, I am using a different username in my database config:

default: &default
  adapter: postgis
  encoding: unicode
  host: <%= ENV['DATABASE_HOST'] %>
  user: <%= ENV['DATABASE_USER'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("DATABASE_POOL") { 10 } %>

The user is not root, and everything works fine from the Rails app itself, which can connect to the database using the user specified in the environment variable.

It just seems that pg_dump, when called from Rails to dump the schema, is not using the proper username from the config, but instead falls back to root.

I see some configuration environment variables in the source code, but how should I set them and why does Rails not do that by default?

I think it works when when, in my docker-compose.yml file I create an additional environment variable PGUSER that has the same value as DATABASE_USER. But that seems like workaround — is this a bug I'm seeing or some expected behavior?

Upvotes: 0

Views: 701

Answers (1)

slhck
slhck

Reputation: 38672

The only solution I found so far was to specify an additional environment variable named PGUSER for the Rails container.

I assume this is a bug or not properly documented.

Upvotes: 2

Related Questions