
postmap Command in Linux
The postmap command in Linux is a Postfix lookup table manager. It is used to create or query lookup tables for various Postfix configurations. These tables store key-value pairs and are used in different Postfix settings, such as alias mappings, access control, or transport rules.
Table of Contents
Here is a comprehensive guide to the options available with the postmap command â
Syntax of postmap Command
The syntax of the postmap command in Linux is as follows:
postmap [options] [file]
In the above syntax, the [options] field is used to mention various options to change the command's output, such as specifying the map key or a custom configuration file. The [file] field is used to specify the Postfix map file.
Options of postmap Command
The options of the postmap command are listed below:
Option | Description |
---|---|
-b | Enables message body query mode. Reads an email message body (RFC 2822) as lookup keys. Stops at the message end unless -m (MIME parsing) is enabled. |
-c config_dir | Uses an alternate configuration directory instead of the default. |
-d key | Removes the specified key from maps. Reads from standard input if - is used as the key. Exits with zero status if at least one key is found. |
-f | Prevents automatic lowercase conversion of lookup keys when creating or querying a table. |
-h | Enables message header query mode. Reads an email message header (RFC 2822) as lookup keys. Stops at the first non-header line unless -m is enabled. |
-i | Enables incremental mode. Reads entries from standard input without truncating an existing database. |
-m | Enables MIME parsing when using -b or -h. |
-N | Includes a terminating null character in lookup keys and values. Defaults to system behavior. |
-n | Excludes a terminating null character in lookup keys and values. Defaults to system behavior. |
-o | Retains root privileges when processing a non-root input file. By default, privileges are dropped. |
-p | Creates a new file with default permissions (0644) instead of inheriting access permissions from the input file. |
-q key | Searches for a key in maps and outputs the first found value. Reads from standard input if - is used. Exits with zero status if at least one key is found. |
-r | Updates a table without error messages when modifying existing entries. |
-s | Retrieves all database elements and outputs them in database order. |
-v | Enables verbose logging for debugging. Multiple -v options increase verbosity. |
-w | Updates a table without error messages when modifying existing entries but ignores those updates. |
Examples of postmap Command in Linux
This section explores how to use the postmap command in Linux with examples:
Generating a Hash Lookup Table
To generate a lookup table first create a text file with alias mapping −
echo "[email protected] user1" | sudo tee /etc/postfix/virtual

Now to create .db file, use the following command:
sudo postmap /etc/postfix/virtual

The above command creates /etc/postfix/virtual.db, which Postfix uses as shown in the above output image.
Querying the Lookup Table
To query the lookup table, use the -q option. For example, to find the value against a key, use the postmap command in the following way:
sudo postmap -q [email protected] /etc/postfix/virtual

Listing All Entries
To display all the key-value pairs, use the -s option with the postmap command:
sudo postmap -s /etc/postfix/virtual

Deleting an Entry
To delete an entry, use the -d option. The following example deletes the [email protected] from the table:
sudo postmap -d [email protected] /etc/postfix/virtual

As it can be seen in the output image, the [email protected] entry has removed.
Creating a Transport Map
To create a transport map, first create the transport file using the following command:
echo "test.com smtp:[mail.test.net]" | sudo tee /etc/postfix/transport
To process the map, use the postmap command:
sudo postmap /etc/postfix/transport
Creating an Access Map
To create an access map using the postmap command first create a text file in the /etc/postfix directory:
echo "192.168.1.1 REJECT" | sudo tee /etc/postfix/access

Now, process the map using the following command:
sudo postmap /etc/postfix/access
To use the restrictions add it to main.cf file:
smtpd_client_restrictions = hash:/etc/postfix/access

Creating a B-Tree Database
The btree maintains sorted keys for efficient range queries in large datasets, while hash provides faster single-key lookups for smaller datasets. By default, the database type is set to hash. To create a btree database, specify the btree explicitly with the postmap command:
sudo postmap btree:/etc/postfix/access
Using the above methods other types of databases such as cdb, dbm, or sdbm can also be created.
Conclusion
The postmap command in Linux is an essential tool for managing lookup tables in Postfix configurations. It allows for creating, querying, and modifying key-value pairs used for alias mappings, access control, and transport rules. Various options enhance its functionality, including querying specific keys, listing all entries, and deleting or updating records. Examples demonstrate its usage in generating hash lookup tables, B-tree databases, transport maps, and access maps.