<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 05/03/2014 01:50 PM, Lutz Gehlen
wrote:<br>
</div>
<blockquote cite="mid:201405032150.06783.lrg_ml@gmx.net" type="cite">
<pre wrap="">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?
</pre>
</blockquote>
<br>
In general, you should use .pgpass for storing Postgres connection
credentials. See the <a
href="http://www.postgresql.org/docs/9.3/static/libpq-pgpass.html">Postgres
docs</a> 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.<br>
<br>
Additionally, you should be able to use* a <a
href="http://www.postgresql.org/docs/9.3/static/libpq-pgservice.html">connection
service file</a> to configure different connection parameters,
e.g. point to the development vs. production database. <a
href="http://stackoverflow.com/a/19980156/176646">This
StackOverflow answer</a> provides a good example using DBI in a
regular Perl script.<br>
<br>
* I've used .pgpass but never .pg_service.conf<br>
<br>
To configure Dancer::Plugin::Database to use the connections from
.pg_service.conf, your configuration should look something like:<br>
<br>
plugins:<br>
Database:<br>
dsn: 'dbi:Pg:service=test'<br>
dbi_params:<br>
RaiseError: 1<br>
AutoCommit: 1<br>
on_connect_do: ...<br>
<br>
(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)<br>
<br>
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:<br>
<br>
plugins:<br>
Database:<br>
dsn:
'dbi:mysql:;mysql_read_default_file=/path/to/database.cfg'<br>
dbi_params:<br>
RaiseError: 1<br>
AutoCommit: 1<br>
<br>
Hope this is useful to you.<br>
</body>
</html>