TheRobotLearns Platform · ProxySQL: from first install to production

PostgreSQL Users and Frontend Access

  • about 15 min read

Learning objectives

  • Create pgsql_users rows assigning frontend, backend, username/password, and active.
  • Set default_hostgroup so unmatched user traffic routes to a target hostgroup.
  • Apply transaction_persistent, fast_forward, and max_connections to control stickiness, bypass, and user connection limits.
  • Create matching PostgreSQL users and test client connections to ProxySQL port 6133.

Why the user cannot connect

You create appuser on your PostgreSQL server, then run psql -h 127.0.0.1 -p6133 -U appuser -d mydatabase. That client path needs a row in pgsql_users. This table defines the PostgreSQL users clients can use to connect to ProxySQL and then to backend PostgreSQL servers. Each row has fields for the username, password, whether the row is active, and where default traffic goes.

ProxySQL keeps those rows in in-memory configuration tables. You change pgsql_users there, then run LOAD PGSQL USERS TO RUNTIME so the running proxy uses the row. A restart needs SAVE PGSQL USERS TO DISK to persist the change. ProxySQL also exposes a PostgreSQL-compatible admin interface on port 6132.

For a new application user, create appuser in the primary PostgreSQL server first. Then insert a matching ProxySQL row, set the fields that decide authentication and routing, load it, and test the client command on port 6133.

Takeaway: Add a loaded pgsql_users row, then test the user on port 6133.

Fields that control access

pgsql_users has many fields, but a few decide who can connect and where queries go.

  • username: the name used to authenticate to the PostgreSQL server.
  • password: the password for that name, stored in plain text or hashed.
  • active: if 0, the row is tracked in the database but never loaded into the in-memory data structures.
  • backend: if 1, the username and password are used for authenticating to backend PostgreSQL servers.
  • frontend: if 1, the username and password are used for authenticating to ProxySQL itself.
  • default_hostgroup: the hostgroup where ProxySQL routes traffic when no matching query rule is found.
  • transaction_persistent: if 1 for a frontend user, transactions started within a hostgroup remain in that hostgroup regardless of other rules.
  • fast_forward: if 1, ProxySQL bypasses query processing, including caching and rewriting, and passes the query directly to the backend server.
  • max_connections: the maximum number of allowable frontend connections for that user.

Currently, all PostgreSQL users require both frontend and backend to be 1. The sample appuser row uses default_hostgroup value 1.

Takeaway: frontend, backend, active, default_hostgroup, and max_connections control authentication, loading, routing, and user connection limits.

Add and apply a user row

You create the matching PostgreSQL user first. In your primary PostgreSQL server, run CREATE USER appuser WITH PASSWORD 'appuser'; and grant access to mydatabase. Then connect to ProxySQL Admin on port 6132 with psql.

The sample row uses password appuser, sets active=1, and sets both backend and frontend to 1. That follows the current rule that PostgreSQL users need both flags. default_hostgroup=1 sends traffic with no matching query rule to hostgroup 1.

The next block adds that row. After the INSERT, the SELECT returns your new row from pgsql_users, and the following commands make the row live and persistent.

INSERT INTO pgsql_users (username, password, active, backend, frontend, default_hostgroup) VALUES ('appuser', 'appuser', 1, 1, 1, 1);
SELECT * FROM pgsql_users;
LOAD PGSQL USERS TO RUNTIME;
SAVE PGSQL USERS TO DISK;

Now test the frontend connection. ProxySQL routes application traffic through the PostgreSQL interface on default port 6133. This command connects through ProxySQL to mydatabase using appuser.

psql -h 127.0.0.1 -p6133 -U appuser -d mydatabase

Takeaway: Insert the pgsql_users row, LOAD it to runtime, save it to disk, then test port 6133.

Common user-row mistakes

If active=0, ProxySQL keeps the row in the database but never loads it into the in-memory data structures. frontend=1 lets the username and password authenticate to ProxySQL. backend=1 lets the same pair authenticate to backend PostgreSQL servers. Currently, all PostgreSQL users require both fields set to 1.

default_hostgroup routes traffic when no matching query rule is found. If transaction_persistent=1 for a frontend user, transactions started within a hostgroup remain in that hostgroup regardless of other rules. If fast_forward=1, ProxySQL bypasses query processing, including caching and rewriting, and passes the query directly to the backend server. If max_connections is set, it defines the maximum number of allowable frontend connections for that user.

PostgreSQL users defined in pgsql_users should not be used for admin-admin_credentials or admin-stats_credentials.

Takeaway: active=0 blocks loading, both frontend and backend must be 1, and default_hostgroup applies only without matching rules.

Add appuser

-- run from psql -h 127.0.0.1 -p6132 -U admin -d admin
INSERT INTO pgsql_users (username, password, active, backend, frontend, default_hostgroup) VALUES ('appuser', 'appuser', 1, 1, 1, 1);
SELECT * FROM pgsql_users;
LOAD PGSQL USERS TO RUNTIME;
SAVE PGSQL USERS TO DISK;

INSERT INTO pgsql_users ... adds a row named appuser with password appuser, active=1, backend=1, frontend=1, and default_hostgroup=1.

SELECT * FROM pgsql_users reads the in-memory configuration table and returns that row.

LOAD PGSQL USERS TO RUNTIME applies the active row to the running proxy.

SAVE PGSQL USERS TO DISK persists the row across restarts.

Try it: config_layer_sim

Connect to ProxySQL Admin with psql -h 127.0.0.1 -p6132 -U admin -d admin.

Create appuser in your primary PostgreSQL server with CREATE USER appuser WITH PASSWORD 'appuser';, then grant access to mydatabase.

Insert a row into pgsql_users for appuser with active=1, backend=1, frontend=1, and default_hostgroup=1.

Run SELECT * FROM pgsql_users; to read the in-memory configuration table.

Run LOAD PGSQL USERS TO RUNTIME; to apply active rows to the running proxy.

Run SELECT * FROM runtime_pgsql_users; to see the loaded row in the runtime projection.

Run SAVE PGSQL USERS TO DISK; to persist the row.

Connect through ProxySQL with psql -h 127.0.0.1 -p6133 -U appuser -d mydatabase.

Sources

Do more with an account

PostgreSQL Users and Frontend Access — ProxySQL: from first install to production