Pg Protocol

Published
Length
3 min read

Luis Felipe Dias

All posts

The pg protocol is the wire rule set that connects client tools to PostgreSQL servers. It sends login checks, SQL queries, and row results back and forth over TCP sockets. Most teams set up this link by giving users direct network routes and database passwords. When teams share broad database roles, one bad query can expose or wipe vital records. How the pg protocol works from login to query Every session starts when a client opens a TCP socket to the database server. The client sends a start

Model Context Protocol (MCP) Security: the complete guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free white paper. No spam. Unsubscribe anytime.

The pg protocol is the wire rule set that connects client tools to PostgreSQL servers. It sends login checks, SQL queries, and row results back and forth over TCP sockets. Most teams set up this link by giving users direct network routes and database passwords. When teams share broad database roles, one bad query can expose or wipe vital records.

How the pg protocol works from login to query

Every session starts when a client opens a TCP socket to the database server. The client sends a startup packet with the user name and target database name. The server then checks its host rules and asks the client for a password. Before checking secrets, the client and server can agree to wrap the link in TLS.

Once the user logs in, the server sends back session details and a ready notice. The session now enters the main command loop. In the simple query flow, the client sends a plain text SQL string. The server reads the string, builds a plan, runs it, and streams the rows back.

Apps also use the extended query flow to run fast, safe statements. This flow splits the work into parse, bind, and execute steps. Separating the SQL structure from the input data stops SQL injection bugs cold. At the end of each step, the server tells the client if the transaction succeeded.

How to set up native wire security

Database admins lock down wire traffic by tuning server files and network rules. First, you require TLS on the server so plain text traffic gets rejected right away. This stops attackers on the local network from reading passwords or row data in transit. You can also check client certificates to verify who is connecting.

Next, you restrict which IP ranges can reach the database port on your network. You pair these network blocks with strong password hashes inside the database engine. Finally, you create distinct roles with read or write rules for specific tables and schemas. Each app or team member gets a role that matches their daily job.

Model Context Protocol (MCP) Security: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Where native wire rules fall short in practice

Native server rules decide who can log in and which tables they can read. Once a session starts, the wire link passes every valid command for that role. If an engineer has read rights on a customer table, the server returns every column. Native rules do not rewrite sensitive fields like emails, phone numbers, or card numbers in flight.

Shared roles also make auditing hard when many people debug live issues. Database logs show the shared role name rather than the specific person at the keyboard. Revoking access takes manual work to rotate secrets and update team tools. Plus, an approved user can still run a drop table command without a second check.

Add runtime guardrails and masking beside your databases and APIs

Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits beside the service as an open-source proxy, a sidecar for your database or API.

Before a query runs, hoop.dev guardrails check the statement against your rules. An AI Analyzer you configure can score intent or hold risky calls for human review. Next, hoop.dev data masking rewrites sensitive fields in responses, and each verdict is logged per listener. Explore the MIT-licensed project on GitHub.

Frequently asked questions

Does the pg protocol encrypt traffic by default?

No, the wire format starts in plain text unless the client and server negotiate TLS. You must enforce TLS in your server rules to block unencrypted logins.

What is the difference between simple and extended query flows?

Simple flow sends one text string that the server parses and runs right away. Extended flow separates parsing and data binding, which improves speed and blocks SQL injection.

Can native database roles mask specific columns on the wire?

Standard roles grant or block access to whole tables or columns. They do not dynamically rewrite national IDs or card numbers inside query results.

Save the open-source gateway.

hoop is MIT-licensed infrastructure for controlling how AI agents reach production data. Star hoophq/hoop so you can inspect it, deploy it, or share it when your team starts governing agent access.