Environment variables in pgsql connect string

Eirik Rye rye at domeneshop.no
Wed Dec 4 14:36:55 EET 2019


I figured this out just moments after writing to this list, actually.

libpq, the Postgresql Library used by Dovecot, will actually use the 
environment variables listed below if their corresponding parameters are 
not provided in the connection string:

   https://www.postgresql.org/docs/8.1/libpq-envars.html

As such, these modifications will allow postgres connection using 
environment variables.

dovecot.conf:

   import_environment = PGUSER PGDATABASE PGPASSWORD

dovecot-sql.conf.ext:

   connect = host=mail-passdb.mail.svc.cluster.local

Then, set the env variables PGUSER, PGDATABASE, PGPASSWORD in the container.

Best regards,
Eirik Rye

On 04/12/2019 13:22, Eirik Rye via dovecot wrote:
> Hi,
> 
> We are trying to set up a Dovecot Docker image with dynamic database 
> configuration based on environment variables, however the variables do 
> not appear to be expanded in the "connect=" string of the SQL config.
> 
> The documentation states that environment variables, being listed under 
> global variables, work "everywhere":
> 
> 
> https://doc.dovecot.org/configuration_manual/config_file/config_variables/#variables-global 
> 
> 
> However, the source code for driver-pgsql.c indicates that the connect 
> string is never expanded:
> 
> 
> https://github.com/dovecot/core/blob/master/src/lib-sql/driver-pgsql.c#L297
> 
> For reference, this is my configuration. dovecot.conf:
> 
>    import_environment = PASSDB_USER PASSDB_DBNAME PASSDB_PASSWORD
>    passdb {
>      override_fields = proxy=y
>      driver = sql
>      args = /etc/dovecot/dovecot-sql.conf.ext
>    }
> 
> /etc/dovecot/dovecot-sql.conf.ext:
> 
>    driver = pgsql
>    connect = host=mail-passdb dbname=%{env:PASSDB_DBNAME} 
> user=%{env:PASSDB_USER} password=%{env:PASSDB_PASSWORD}
> 
> Does anyone have any suggestions for how to connect to a database 
> without hardcoding the connection parameters in the Dovecot config, 
> which does not involve patching driver-pgsql.c to make the variable 
> expansion?
> 
> Best regards,
> Eirik Rye


-- 
Eirik


More information about the dovecot mailing list