PostgreSQL

PostgreSQL authentication uses an external PostgreSQL database as the authentication data source, which can store a large amount of data and facilitate integration with external device management systems.

Plugin:

  1. emqx_auth_pgsql

TIP

The emqx_auth_pgsql also includes ACL feature, which can be disabled via comments

To enable PostgreSQL authentication, you need to configure the following in etc/plugins/emqx_auth_pgsql.conf

PostgreSQL connection information

For PostgreSQL basic connection information, it needs to ensure that all nodes in the cluster can access.

  1. # etc/plugins/emqx_auth_pgsql.conf
  2. ## Server address
  3. auth.pgsql.server = 127.0.0.1:5432
  4. ## Connection pool size
  5. auth.pgsql.pool = 8
  6. auth.pgsql.username = root
  7. auth.pgsql.password = public
  8. auth.pgsql.database = mqtt
  9. auth.pgsql.encoding = utf8
  10. ## TLS configuration
  11. ## auth.pgsql.ssl = false
  12. ## auth.pgsql.ssl_opts.keyfile =
  13. ## auth.pgsql.ssl_opts.certfile =

Default table structure

In the default configuration of PostgreSQL authentication, you need to ensure that the following table is in the database:

  1. CREATE TABLE mqtt_user (
  2. id SERIAL primary key,
  3. is_superuser boolean,
  4. username character varying(100),
  5. password character varying(100),
  6. salt character varying(40)
  7. )

The sample data in the default configuration is as follows:

  1. INSERT INTO mqtt_user (username, password, salt, is_superuser)
  2. VALUES
  3. ('emqx', 'efa1f375d76194fa51a3556a97e641e61685f914d446979da50a551a4333ffd7', NULL, false);

After PostgreSQL authentication is enabled, you can connect with username: emqx, password: public.

TIP

This is the table structure used by default configuration. After being familiar with the use of the plugin, you can use any data table that meets the conditions for authentication

Salting rules and hash methods

PostgreSQL authentication support to configure Salting rules and hash methods

  1. # etc/plugins/emqx_auth_pgsql.conf
  2. auth.pgsql.password_hash = sha256

auth_query

During authentication, EMQ X Broker will use the current client information to populate and execute the user-configured authentication SQL to query the client’s authentication data in the database.

  1. # etc/plugins/emqx_auth_pgsql.conf
  2. auth.pgsql.auth_query = select password from mqtt_user where username = '%u' limit 1

You can use the following placeholders in the SQL authentication, and EMQ X Broker will be automatically populated with client information when executed:

  • %u:Username
  • %c:Client ID
  • %C:TLS certificate common name (the domain name or subdomain name of the certificate), valid only for TLS connections
  • %d:TLS certificate subject, valid only for TLS connections

You can adjust the authentication SQL according to business to achieve more business-related functions, such as adding multiple query conditions and using database preprocessing functions. However, in any case, the authentication must meet the following conditions:

  1. The query result must include the password field, which is used by EMQ X Broker to compare with the client password
  2. If the salting configuration is enabled, the query result must include the salt field, which is used by EMQ X Broker as the salt value
  3. There can only be one query result. When there are multiple results, only the first one is taken as valid data.

TIP

You can use AS syntax in SQL to specify passwords for field renaming, or set the salt value to a fixed value.