[dancer-users] Flexible credentials with Dancer::Plugin::Database

Maxwell Carey mcarey at ucar.edu
Mon May 5 19:33:08 BST 2014

On 05/03/2014 01:50 PM, Lutz Gehlen wrote:
> Hello all,
> I would like to use Dancer::Plugin::Database to access a postgresql
> database from my dancer application. The docu suggests to configure the
> connection like this:
> plugins:
>      Database:
>          driver: 'mysql'
>          database: 'test'
>          host: 'localhost'
>          port: 3306
>          username: 'myusername'
>          password: 'mypassword'
>          connection_check_threshold: 10
>          dbi_params:
>              RaiseError: 1
>              AutoCommit: 1
>          on_connect_do: ["SET NAMES 'utf8'", "SET CHARACTER SET 'utf8'" ]
>          log_queries: 1
>          handle_class: 'My::Super::Sexy::Database::Handle'
> However, I would like to store the login details, namely username and
> password, in a different file that I can exclude from version control by
> gitignore for security reasons as well as to enable my co-developers to use
> different credentials on their system. Is there a good way to achieve this?

In general, you should use .pgpass for storing Postgres connection 
credentials. See the Postgres docs 
<http://www.postgresql.org/docs/9.3/static/libpq-pgpass.html> for 
details. This allows you to save credentials in one place without having 
to hard-code them into every application or config file that needs to 
access the database.

Additionally, you should be able to use* a connection service file 
<http://www.postgresql.org/docs/9.3/static/libpq-pgservice.html> to 
configure different connection parameters, e.g. point to the development 
vs. production database. This StackOverflow answer 
<http://stackoverflow.com/a/19980156/176646> provides a good example 
using DBI in a regular Perl script.

* I've used .pgpass but never .pg_service.conf

To configure Dancer::Plugin::Database to use the connections from 
.pg_service.conf, your configuration should look something like:

         dsn: 'dbi:Pg:service=test'
             RaiseError: 1
             AutoCommit: 1
         on_connect_do: ...

(assuming you have a service named "test" in .pg_service.conf, as in the 
StackOverflow answer I cited earlier. You would presumably put the above 
in environments/development.yml)

I haven't tried this with Postgres before so YMMV, but I do something 
similar with MySQL to allow non-Dancer apps and Dancer apps to share the 
same credentials file:

         dsn: 'dbi:mysql:;mysql_read_default_file=/path/to/database.cfg'
             RaiseError: 1
             AutoCommit: 1

Hope this is useful to you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20140505/1cf99b80/attachment.html>

More information about the dancer-users mailing list