This project is a PostgreSQL driver for Node.js, written entirely from scratch.
It aims to provide a minimal, transparent, and educational implementation of the PostgreSQL Protocol
It demonstrates how to connect, handle authentication, parse protocol messages, and execute SQL queries without using any external libraries for PostgreSQL.
- Connects to a PostgreSQL server using Node.js's
netmodule - Authenticates using cleartext password
- Parses key protocol messages:
AuthenticationOkParameterStatusBackendKeyDataReadyForQueryRowDescriptionDataRow
- Sends basic SQL queries (e.g.,
SELECT now();) - Decodes and prints result rows and column descriptions
The client follows these steps:
- Startup: Sends a startup message with user and database.
- Authentication: Handles cleartext password authentication.
- Session Setup: Receives server parameters (
ParameterStatus) and backend info (BackendKeyData). (NOTE: we are not using as of NOW) - Ready State: Waits for
ReadyForQuerybefore sending SQL queries. - Query: Sends a SQL query and parses the response (column info and data row).
- Result Parsing: Extracts and prints column names and result values.
-
Clone the repository:
git clone https://github.com/sanjay-xdr/pg-driver cd pg-driver npm i -
Edit connection settings: Open
pg-protocol.tsand set your PostgreSQL credentials:const config = { host: "localhost", port: 5432, password: "sanjay", userName: "postgres", db: "Hotel", };
-
Build and Run the client:
npm run build npm run start
You should see output for the connection, authentication, and query results (e.g., current date/time).
RESULT { now: '2025-05-30 13:10:54.039803+05:30' }
Query Exectued SELECT 1