1<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" 2 "http://www.w3.org/TR/html4/loose.dtd"> 3 4<html> 5 6<head> 7 8<title>Postfix PostgreSQL Howto</title> 9 10<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 11<link rel='stylesheet' type='text/css' href='postfix-doc.css'> 12 13</head> 14 15<body> 16 17<h1><img src="postfix-logo.jpg" width="203" height="98" ALT="">Postfix PostgreSQL Howto</h1> 18 19<hr> 20 21<h2>Introduction</h2> 22 23<p> The Postfix pgsql map type allows you to hook up Postfix to a 24PostgreSQL database. This implementation allows for multiple pgsql 25databases: you can use one for a virtual(5) table, one for an 26access(5) table, and one for an aliases(5) table if you want. You 27can specify multiple servers for the same database, so that Postfix 28can switch to a good database server if one goes bad. </p> 29 30<p> Busy mail servers using pgsql maps will generate lots of 31concurrent pgsql clients, so the pgsql server(s) should be run with 32this fact in mind. You can reduce the number of concurrent pgsql 33clients by using the Postfix proxymap(8) service. </p> 34 35<h2>Building Postfix with PostgreSQL support</h2> 36 37<p> These instructions assume that you build Postfix from source 38code as described in the INSTALL document. Some modification may 39be required if you build Postfix from a vendor-specific source 40package. </p> 41 42<p> Note: to use pgsql with Debian GNU/Linux's Postfix, all you 43need to do is to install the postfix-pgsql package and you're done. 44There is no need to recompile Postfix. </p> 45 46<p> In order to build Postfix with pgsql map support, you specify 47-DHAS_PGSQL, the directory with the PostgreSQL header files, and 48the location of the libpq library file. </p> 49 50<p> For example: </p> 51 52<blockquote> 53<pre> 54% make tidy 55% make -f Makefile.init makefiles \ 56 "CCARGS=-DHAS_PGSQL -I/usr/local/include/pgsql" \ 57 "AUXLIBS_PGSQL=-L/usr/local/lib -lpq" 58</pre> 59</blockquote> 60 61<p> If your PostgreSQL shared library is in a directory that the RUN-TIME 62linker does not know about, add a "-Wl,-R,/path/to/directory" option after 63"-lpq". </p> 64 65<p> Postfix versions before 3.0 use AUXLIBS instead of AUXLIBS_PGSQL. 66With Postfix 3.0 and later, the old AUXLIBS variable still supports 67building a statically-loaded PostgreSQL database client, but only 68the new AUXLIBS_PGSQL variable supports building a dynamically-loaded 69or statically-loaded PostgreSQL database client. </p> 70 71<blockquote> 72 73<p> Failure to use the AUXLIBS_PGSQL variable will defeat the purpose 74of dynamic database client loading. Every Postfix executable file 75will have PGSQL database library dependencies. And that was exactly 76what dynamic database client loading was meant to avoid. </p> 77 78</blockquote> 79 80<p> Then just run 'make'. </p> 81 82<h2>Configuring PostgreSQL lookup tables</h2> 83 84<p> Once Postfix is built with pgsql support, you can specify a 85map type in main.cf like this: </p> 86 87<blockquote> 88<pre> 89/etc/postfix/main.cf: 90 alias_maps = pgsql:/etc/postfix/pgsql-aliases.cf 91</pre> 92</blockquote> 93 94<p> The file /etc/postfix/pgsql-aliases.cf specifies lots of 95information telling postfix how to reference the pgsql database. 96For a complete description, see the pgsql_table(5) manual page. </p> 97 98<h2>Example: local aliases </h2> 99 100<pre> 101# 102# pgsql config file for local(8) aliases(5) lookups 103# 104 105# 106# The hosts that Postfix will try to connect to 107hosts = host1.some.domain host2.some.domain 108 109# The user name and password to log into the pgsql server. 110user = someone 111password = some_password 112 113# The database name on the servers. 114dbname = customer_database 115 116# Postfix 2.2 and later The SQL query template. See pgsql_table(5). 117query = SELECT forw_addr FROM mxaliases WHERE alias='%s' AND status='paid' 118 119# For Postfix releases prior to 2.2. See pgsql_table(5) for details. 120select_field = forw_addr 121table = mxaliases 122where_field = alias 123# Don't forget the leading "AND"! 124additional_conditions = AND status = 'paid' 125</pre> 126 127<h2>Using mirrored databases</h2> 128 129<p> Sites that have a need for multiple mail exchangers may enjoy 130the convenience of using a networked mailer database, but do not 131want to introduce a single point of failure to their system. </p> 132 133<p> For this reason we've included the ability to have Postfix 134reference multiple hosts for access to a single pgsql map. This 135will work if sites set up mirrored pgsql databases on two or more 136hosts. </p> 137 138<p> Whenever queries fail with an error at one host, the rest of 139the hosts will be tried in random order. If no pgsql server hosts 140are reachable, then mail will be deferred until at least one of 141those hosts is reachable. </p> 142 143<h2>Credits</h2> 144 145<ul> 146 147<li> This code is based upon the Postfix mysql map by Scott Cotton 148and Joshua Marcus, IC Group, Inc.</li> 149 150<li> The PostgreSQL changes were done by Aaron Sethman.</li> 151 152<li> Updates for Postfix 1.1.x and PostgreSQL 7.1+ and support for 153calling stored procedures were added by Philip Warner.</li> 154 155<li> LaMont Jones was the initial Postfix pgsql maintainer.</li> 156 157<li> Liviu Daia revised the configuration interface and added the 158main.cf configuration feature.</li> 159 160<li> Liviu Daia revised the configuration interface and added the main.cf 161configuration feature.</li> 162 163<li> Liviu Daia with further refinements from Jose Luis Tallon and 164Victor Duchovni developed the common query, result_format, domain and 165expansion_limit interface for LDAP, MySQL and PosgreSQL.</li> 166 167<li> Leandro Santi updated the PostgreSQL client after the PostgreSQL 168developers made major database API changes in response to SQL 169injection problems, and made PQexec() handling more robust. </li> 170 171</ul> 172 173</body> 174 175</html> 176