1 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" 2 "https://www.w3.org/TR/html4/loose.dtd"> 3 <html> <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <link rel='stylesheet' type='text/css' href='postfix-doc.css'> 6 <title> Postfix manual - pgsql_table(5) </title> 7 </head> <body> <pre> 8 PGSQL_TABLE(5) PGSQL_TABLE(5) 9 10 <b><a name="name">NAME</a></b> 11 pgsql_table - Postfix PostgreSQL client configuration 12 13 <b><a name="synopsis">SYNOPSIS</a></b> 14 <b>postmap -q "</b><i>string</i><b>" <a href="pgsql_table.5.html">pgsql</a>:/etc/postfix/</b><i>filename</i> 15 16 <b>postmap -q - <a href="pgsql_table.5.html">pgsql</a>:/etc/postfix/</b><i>filename</i> <<i>inputfile</i> 17 18 <b><a name="description">DESCRIPTION</a></b> 19 The Postfix mail system uses optional tables for address rewriting or 20 mail routing. These tables are usually in <b><a href="lmdb_table.5.html">lmdb</a>:</b>, <b><a href="CDB_README.html">cdb</a>:</b>, <b><a href="DATABASE_README.html#types">hash</a>:</b>, or <b><a href="DATABASE_README.html#types">dbm</a>:</b> 21 format. 22 23 Alternatively, lookup tables can be specified as PostgreSQL databases. 24 To find out what types of lookup tables your Postfix system supports 25 use the "<b>postconf -m</b>" command. 26 27 In order to use PostgreSQL lookups, define a PostgreSQL source as a 28 lookup table in <a href="postconf.5.html">main.cf</a>, for example: 29 <a href="postconf.5.html#alias_maps">alias_maps</a> = <a href="pgsql_table.5.html">pgsql</a>:/etc/postfix/pgsql-aliases.cf 30 31 The file /etc/postfix/pgsql-aliases.cf has the same format as the Post- 32 fix <a href="postconf.5.html">main.cf</a> file, and can specify the parameters described below. 33 34 <b><a name="list_membership">LIST MEMBERSHIP</a></b> 35 When using SQL to store lists such as $<a href="postconf.5.html#mynetworks">mynetworks</a>, $<a href="postconf.5.html#mydestination">mydestination</a>, 36 $<a href="postconf.5.html#relay_domains">relay_domains</a>, $<a href="postconf.5.html#local_recipient_maps">local_recipient_maps</a>, etc., it is important to under- 37 stand that the table must store each list member as a separate key. The 38 table lookup verifies the *existence* of the key. See "Postfix lists 39 versus tables" in the <a href="DATABASE_README.html">DATABASE_README</a> document for a discussion. 40 41 Do NOT create tables that return the full list of domains in $<a href="postconf.5.html#mydestination">mydesti</a>- 42 <a href="postconf.5.html#mydestination">nation</a> or $<a href="postconf.5.html#relay_domains">relay_domains</a> etc., or IP addresses in $<a href="postconf.5.html#mynetworks">mynetworks</a>. 43 44 DO create tables with each matching item as a key and with an arbitrary 45 value. With SQL databases it is not uncommon to return the key itself 46 or a constant value. 47 48 <b><a name="pgsql_parameters">PGSQL PARAMETERS</a></b> 49 <b>hosts</b> The hosts that Postfix will try to connect to and query from. 50 Besides a PostgreSQL connection URI, this setting supports the 51 historical forms <b>unix:/</b><i>pathname</i> for UNIX-domain sockets and 52 <b>inet:</b><i>host:port</i> for TCP connections, where the <b>unix:</b> and <b>inet:</b> 53 prefixes are accepted and ignored for backwards compatibility. 54 Examples: 55 hosts = postgresql://username (a] example.com/<i>databasename</i>?sslmode=require 56 hosts = postgres://user:secret@localhost 57 hosts = inet:host1.some.domain inet:host2.some.domain:port 58 hosts = host1.some.domain host2.some.domain:port 59 hosts = unix:/file/name 60 61 See <a href="https://www.postgresql.org/docs/current/libpq-connect.html">https://www.postgresql.org/docs/current/libpq-connect.html</a> 62 for the supported connection URI syntax. 63 64 The hosts are tried in random order. The connections are auto- 65 matically closed after being idle for about 1 minute, and are 66 re-opened as necessary. See <b>idle_interval</b> for details. 67 68 NOTE: if the <b>hosts</b> setting specifies a PostgreSQL connection 69 URI, the Postfix PostgreSQL client will ignore the <b>dbname</b>, <b>user</b>, 70 and <b>password</b> settings for that connection. 71 72 NOTE: if the <b>hosts</b> setting specifies only one server, this 73 client assumes that the target is a load balancer and will 74 reconnect immediately after a single failure. With Postfix ver- 75 sions 3.9 and earlier, specify the same server twice. 76 77 <b>user</b> 78 79 <b>password</b> 80 The user name and password to log into the pgsql server. Exam- 81 ple: 82 user = someone 83 password = some_password 84 85 The <b>user</b> and <b>password</b> settings are ignored for <b>hosts</b> connections 86 that are specified as an URI. 87 88 <b>dbname</b> The database name on the servers. Example: 89 dbname = customer_database 90 91 The <b>dbname</b> setting is ignored for <b>hosts</b> connections that are 92 specified as an URI. 93 94 The <b>dbname</b> setting is required with Postfix 3.10 and later, when 95 <b>hosts</b> specifies any non-URI connection; it is always required 96 with earlier Postfix versions. 97 98 <b>encoding</b> 99 The encoding used by the database client. The default setting 100 is: 101 encoding = UTF8 102 103 Historically, the database client was hard coded to use LATIN1 104 in an attempt to disable multibyte character support. 105 106 This feature is available in Postfix 3.8 and later. 107 108 <b>idle_interval (default: 60)</b> 109 The number of seconds after which an idle database connection 110 will be closed. 111 112 This feature is available in Postfix 3.9 and later. 113 114 <b>retry_interval (default: 60)</b> 115 The number of seconds that a database connection will be skipped 116 after an error. 117 118 NOTE: if the <b>hosts</b> setting specifies only one server, this 119 client assumes that the target is a load balancer and will 120 reconnect immediately after a single failure. With Postfix ver- 121 sions 3.9 and earlier, specify the same server twice. 122 123 This feature is available in Postfix 3.9 and later. 124 125 <b>query</b> The SQL query template used to search the database, where <b>%s</b> is 126 a substitute for the address Postfix is trying to resolve, e.g. 127 query = SELECT replacement FROM aliases WHERE mailbox = '%s' 128 129 This parameter supports the following '%' expansions: 130 131 <b>%%</b> This is replaced by a literal '%' character. (Postfix 2.2 132 and later) 133 134 <b>%s</b> This is replaced by the input key. SQL quoting is used 135 to make sure that the input key does not add unexpected 136 metacharacters. 137 138 <b>%u</b> When the input key is an address of the form user@domain, 139 <b>%u</b> is replaced by the SQL quoted local part of the 140 address. Otherwise, <b>%u</b> is replaced by the entire search 141 string. If the localpart is empty, the query is sup- 142 pressed and returns no results. 143 144 <b>%d</b> When the input key is an address of the form user@domain, 145 <b>%d</b> is replaced by the SQL quoted domain part of the 146 address. Otherwise, the query is suppressed and returns 147 no results. 148 149 <b>%[SUD]</b> The upper-case equivalents of the above expansions behave 150 in the <b>query</b> parameter identically to their lower-case 151 counter-parts. With the <b>result_format</b> parameter (see 152 below), they expand the input key rather than the result 153 value. 154 155 The above %S, %U and %D expansions are available with 156 Postfix 2.2 and later 157 158 <b>%[1-9]</b> The patterns %1, %2, ... %9 are replaced by the corre- 159 sponding most significant component of the input key's 160 domain. If the input key is <i>user (a] mail.example.com</i>, then 161 %1 is <b>com</b>, %2 is <b>example</b> and %3 is <b>mail</b>. If the input key 162 is unqualified or does not have enough domain components 163 to satisfy all the specified patterns, the query is sup- 164 pressed and returns no results. 165 166 The above %1, ... %9 expansions are available with Post- 167 fix 2.2 and later 168 169 The <b>domain</b> parameter described below limits the input keys to 170 addresses in matching domains. When the <b>domain</b> parameter is 171 non-empty, SQL queries for unqualified addresses or addresses in 172 non-matching domains are suppressed and return no results. 173 174 The precedence of this parameter has changed with Postfix 2.2, 175 in prior releases the precedence was, from highest to lowest, 176 <b>select_function</b>, <b>query</b>, <b>select_field</b>, ... 177 178 With Postfix 2.2 the <b>query</b> parameter has highest precedence, see 179 OBSOLETE QUERY INTERFACES below. 180 181 NOTE: DO NOT put quotes around the <b>query</b> parameter. 182 183 <b>result_format (default: %s</b>) 184 Format template applied to result attributes. Most commonly used 185 to append (or prepend) text to the result. This parameter sup- 186 ports the following '%' expansions: 187 188 <b>%%</b> This is replaced by a literal '%' character. 189 190 <b>%s</b> This is replaced by the value of the result attribute. 191 When result is empty it is skipped. 192 193 <b>%u</b> When the result attribute value is an address of the form 194 user@domain, <b>%u</b> is replaced by the local part of the 195 address. When the result has an empty localpart it is 196 skipped. 197 198 <b>%d</b> When a result attribute value is an address of the form 199 user@domain, <b>%d</b> is replaced by the domain part of the 200 attribute value. When the result is unqualified it is 201 skipped. 202 203 <b>%[SUD1-9]</b> 204 The upper-case and decimal digit expansions interpolate 205 the parts of the input key rather than the result. Their 206 behavior is identical to that described with <b>query</b>, and 207 in fact because the input key is known in advance, 208 queries whose key does not contain all the information 209 specified in the result template are suppressed and 210 return no results. 211 212 For example, using "result_format = <a href="smtp.8.html">smtp</a>:[%s]" allows one to use 213 a mailHost attribute as the basis of a <a href="transport.5.html">transport(5)</a> table. After 214 applying the result format, multiple values are concatenated as 215 comma separated strings. The expansion_limit and parameter 216 explained below allows one to restrict the number of values in 217 the result, which is especially useful for maps that must return 218 at most one value. 219 220 The default value <b>%s</b> specifies that each result value should be 221 used as is. 222 223 This parameter is available with Postfix 2.2 and later. 224 225 NOTE: DO NOT put quotes around the result format! 226 227 <b>domain (default: no domain list)</b> 228 This is a list of domain names, paths to files, or "<a href="DATABASE_README.html">type:table</a>" 229 databases. When specified, only fully qualified search keys with 230 a *non-empty* localpart and a matching domain are eligible for 231 lookup: 'user' lookups, bare domain lookups and "@domain" 232 lookups are not performed. This can significantly reduce the 233 query load on the PostgreSQL server. 234 domain = postfix.org, <a href="DATABASE_README.html#types">hash</a>:/etc/postfix/searchdomains 235 236 It is best not to use SQL to store the domains eligible for SQL 237 lookups. 238 239 This parameter is available with Postfix 2.2 and later. 240 241 NOTE: DO NOT define this parameter for <a href="local.8.html">local(8)</a> aliases, because 242 the input keys are always unqualified. 243 244 <b>expansion_limit (default: 0)</b> 245 A limit on the total number of result elements returned (as a 246 comma separated list) by a lookup against the map. A setting of 247 zero disables the limit. Lookups fail with a temporary error if 248 the limit is exceeded. Setting the limit to 1 ensures that 249 lookups do not return multiple values. 250 251 <b>OBSOLETE MAIN.CF PARAMETERS</b> 252 For compatibility with other Postfix lookup tables, PostgreSQL parame- 253 ters can also be defined in <a href="postconf.5.html">main.cf</a>. In order to do that, specify as 254 PostgreSQL source a name that doesn't begin with a slash or a dot. The 255 PostgreSQL parameters will then be accessible as the name you've given 256 the source in its definition, an underscore, and the name of the param- 257 eter. For example, if the map is specified as "<a href="pgsql_table.5.html">pgsql</a>:<i>pgsqlname</i>", the 258 parameter "hosts" would be defined in <a href="postconf.5.html">main.cf</a> as "<i>pgsqlname</i>_hosts". 259 260 Note: with this form, the passwords for the PostgreSQL sources are 261 written in <a href="postconf.5.html">main.cf</a>, which is normally world-readable. Support for this 262 form will be removed in a future Postfix version. 263 264 <b><a name="obsolete_query_interfaces">OBSOLETE QUERY INTERFACES</a></b> 265 This section describes query interfaces that are deprecated as of Post- 266 fix 2.2. Please migrate to the new <b>query</b> interface as the old inter- 267 faces are slated to be phased out. 268 269 <b>select_function</b> 270 This parameter specifies a database function name. Example: 271 select_function = my_lookup_user_alias 272 273 This is equivalent to: 274 query = SELECT my_lookup_user_alias('%s') 275 276 This parameter overrides the legacy table-related fields 277 (described below). With Postfix versions prior to 2.2, it also 278 overrides the <b>query</b> parameter. Starting with Postfix 2.2, the 279 <b>query</b> parameter has highest precedence, and the <b>select_function</b> 280 parameter is deprecated. 281 282 The following parameters (with lower precedence than the <b>select_func-</b> 283 <b>tion</b> interface described above) can be used to build the SQL select 284 statement as follows: 285 286 SELECT [<b>select_field</b>] 287 FROM [<b>table</b>] 288 WHERE [<b>where_field</b>] = '%s' 289 [<b>additional_conditions</b>] 290 291 The specifier %s is replaced with each lookup by the lookup key and is 292 escaped so if it contains single quotes or other odd characters, it 293 will not cause a parse error, or worse, a security problem. 294 295 Starting with Postfix 2.2, this interface is obsoleted by the more gen- 296 eral <b>query</b> interface described above. If higher precedence the <b>query</b> or 297 <b>select_function</b> parameters described above are defined, the parameters 298 described here are ignored. 299 300 <b>select_field</b> 301 The SQL "select" parameter. Example: 302 <b>select_field</b> = forw_addr 303 304 <b>table</b> The SQL "select .. from" table name. Example: 305 <b>table</b> = mxaliases 306 307 <b>where_field</b> 308 The SQL "select .. where" parameter. Example: 309 <b>where_field</b> = alias 310 311 <b>additional_conditions</b> 312 Additional conditions to the SQL query. Example: 313 <b>additional_conditions</b> = AND status = 'paid' 314 315 <b><a name="see_also">SEE ALSO</a></b> 316 <a href="postmap.1.html">postmap(1)</a>, Postfix lookup table manager 317 <a href="postconf.5.html">postconf(5)</a>, configuration parameters 318 <a href="ldap_table.5.html">ldap_table(5)</a>, LDAP lookup tables 319 <a href="mysql_table.5.html">mysql_table(5)</a>, MySQL lookup tables 320 <a href="sqlite_table.5.html">sqlite_table(5)</a>, SQLite lookup tables 321 322 <b><a name="readme_files">README FILES</a></b> 323 <a href="DATABASE_README.html">DATABASE_README</a>, Postfix lookup table overview 324 <a href="PGSQL_README.html">PGSQL_README</a>, Postfix PostgreSQL client guide 325 326 <b><a name="license">LICENSE</a></b> 327 The Secure Mailer license must be distributed with this software. 328 329 <b><a name="history">HISTORY</a></b> 330 PgSQL support was introduced with Postfix version 2.1. 331 332 <b>AUTHOR(S)</b> 333 Based on the MySQL client by: 334 Scott Cotton, Joshua Marcus 335 IC Group, Inc. 336 337 Ported to PostgreSQL by: 338 Aaron Sethman 339 340 Further enhanced by: 341 Liviu Daia 342 Institute of Mathematics of the Romanian Academy 343 P.O. BOX 1-764 344 RO-014700 Bucharest, ROMANIA 345 346 PGSQL_TABLE(5) 347 </pre> </body> </html> 348