title: Perl - DBI and .pgpass
body: <p>I can successfully create a connection to a Postgres db using the following: </p>
  
  <pre><code>my $settings = {
      host =&gt; 'myhost',
      db =&gt; 'mydb',
      user =&gt; 'myuser',
      passwd =&gt; 'mypasswd'
  };
  
  my $connection = DBI-&gt;connect(
      'DBI:Pg:dbname=' . $settings-&gt;{'db'} . ';host=' . $settings-&gt;{'host'},
      $settings-&gt;{'user'},
      $settings-&gt;{'passwd'},
      {
          RaiseError =&gt; 1,
          ShowErrorStatement =&gt; 0,
          AutoCommit =&gt; 0
      }
  ) or die DBI-&gt;errstr;
  </code></pre>
  
  <p>But I'm left with valuable login credentials exposed (yes, I changed them) in my Perl module. Currently, I use <code>psql</code> to issue queries interactively. And to save on having to remember my username/password, I have placed the credentials in a file (~/.pgpass) with permissions 600. The file looks like this:</p>
  
  <pre><code># host:port:database:user:passwd
  myhost:5432:mydb:myuser:mypasswd
  </code></pre>
  
  <p>How can I safely use this file (<code>"$ENV{HOME}/.pgpass"</code>) and the <code>DBI</code> module to hide my credentials? Can it be done? What is best practice?</p>