title: password authentication failed for user "root" when dumping Rails schema as SQL
body: <p>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 <code>config/application.rb</code>:</p>
  
  <pre><code>config.active_record.schema_format = :sql
  </code></pre>
  
  <p>So that special features in the Schema can get dumped. However, doing so during <code>db:migrate</code>, I get the following error:</p>
  
  <blockquote>
    <p>pg_dump: [archiver (db)] connection to database "development" failed: FATAL:  password authentication failed for user "root"</p>
  </blockquote>
  
  <p>The command that gets executed is:</p>
  
  <p><code>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</code></p>
  
  <p>The reason for the error is that there is no <code>root</code> user here; in fact, I am using a different username in my database config:</p>
  
  <pre><code>default: &amp;default
    adapter: postgis
    encoding: unicode
    host: &lt;%= ENV['DATABASE_HOST'] %&gt;
    user: &lt;%= ENV['DATABASE_USER'] %&gt;
    password: &lt;%= ENV['DATABASE_PASSWORD'] %&gt;
    # For details on connection pooling, see Rails configuration guide
    # http://guides.rubyonrails.org/configuring.html#database-pooling
    pool: &lt;%= ENV.fetch("DATABASE_POOL") { 10 } %&gt;
  </code></pre>
  
  <p>The user is <em>not</em> <code>root</code>, and everything works fine from the Rails app itself, which can connect to the database using the user specified in the environment variable.</p>
  
  <p>It just seems that <code>pg_dump</code>, when called from Rails to dump the schema, is not using the proper username from the config, but instead falls back to <code>root</code>.</p>
  
  <p>I see some configuration <a href="https://github.com/rails/rails/blob/98a57aa5f610bc66af31af409c72173cdeeb3c9e/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb#L105" rel="nofollow noreferrer">environment variables in the source code</a>, but how should I set them and why does Rails not do that by default?</p>
  
  <p>I think it works when when, in my <code>docker-compose.yml</code> file I create an additional environment variable <code>PGUSER</code> that has the same value as <code>DATABASE_USER</code>. But that seems like workaround — is this a bug I'm seeing or some expected behavior?</p>