Query your Postgres databases directly using gRPC or transcoded JSON.

Overview

PostgRPC

Query your Postgres databases directly using gRPC or transcoded JSON.

Crates

Postguard

Latest Version Documentation

PostgRPC

(release pending)

postgres-services

(release pending)

postgres-pool

(release pending)

postgres-role-json-pool

(release pending)

postgres-transaction-pool

(release pending)

Table of Contents

  1. Introduction
    1. Why
    2. Similar Projects
    3. Goals
    4. Non-Goals
  2. Getting Started
    1. Installation
    2. Configuration
    3. Usage
  3. Examples
    1. JSON Transcoding
    2. Load Balancing
    3. Auth
  4. FAQ

Introduction

Why?

Sometimes you want to use the full power of a Postgres database, but you aren't able to make direct connections or you don't want to write a custom API service that wraps your database queries. PostgRPC gives you the power of SQL over gRPC or JSON, handling distributed transactions and connection management on your behalf.

Similar Projects

PostgRPC fills a similar niche as the excellent PostgREST and PostGraphile projects. Unlike those projects, PostgRPC lets you use SQL directly rather than wrapping the interface in another query language (i.e. a REST DSL or GraphQL, respectively). In addition, PostgRPC lets you work with lower-level database constructs like transactions through the same gRPC or JSON interface used to query your database.

Goals

  • Performance: running a query over a persistent connection will always be the fastest option, but using PostgRPC should be the next-best option. Where concurrent queries are needed, and where those queries scale up faster than connections, PostgRPC should handle more concurrent query requests than any other direct-connection-based connection pool solution.
  • Primitive Focus: where Postgres has a feature, PostgRPC should support that feature through the query interface. Where this is impossible, PostgRPC should strive to provide a distributed equivalent.
  • Ease-of-Use: those looking to get started with PostgRPC should be able to spin it up as a service quickly on a variety of systems. PostgRPC should ship with sane defaults for most use-cases.
  • Type Inference: PostgRPC should accomdate the flexibility of JSON in inputs and outputs rather than mapping JSON or gRPC types to Postgres types. This includes leveraging Postgres's built-in type inference wherever possible.
  • Customization: PostgRPC should be a reference implementation of a gRPC service that can be easily re-implemented over custom connection pool logic. postgres-services should be usable by anyone looking to use a different protocol to query connection pools that implement the Pool and Connection traits from postgres-pool.

Non-Goals

  • Auth: PostgRPC does not include authentication or authorization mechanisms beyond those provided by the Postgres database itself. Setting the Postgres ROLE can be done through an X-Postgres-Role header, and correctly deriving the value of that header is the responsibility of other services better suited to the task (e.g. Ory Oathkeeper)
  • Strict Request Types: PostgRPC will not support binary encoding of custom or extension-provided types. Instead, all types will be inferred or coerced through Postgres itself. Where type inference or coercion is unsufficient, queries should use text parameters and type hints in the query itself to convert values appropriately.
  • All-in-One: PostgRPC does not replace your application stack. Instead, PostgRPC is a sharp tool that's easy to integrate into a toolbox that includes things like user management, load balancing, and routing of traffic from public endpoints. Do not expose your database publicly through PostgRPC unless you know what you're doing (and even then, consider alternatives like those found in the examples directory).

Getting Started

Installation

Make sure that you have cargo and appropriate Rust toolchains installed. Then clone this repo and run cargo build --release in the postgrpc subdirectory.

The final executable to run will be at ./target/release/postgrpc.

Configuration

PostgRPC can be configured with the following environment variables:

  • HOST: the host that the postgrpc service uses. Defaults to 127.0.0.1.
  • PORT: the port that the postgrpc service uses. Defaults to 50051.
  • TERMINATION_PERIOD: the number of milliseconds postgrpc waits before shutting down on SIGTERM signals. postgrpc shuts down gracefully, waiting for requests to finish where possible. This value is useful for waiting for proxies like envoy to drain, allowing postgrpc to handle those requests without error as long as they take less than TERMINATION_PERIOD milliseconds.
  • STATEMENT_TIMEOUT: the number of milliseconds postgrpc waits before aborting queries.
  • PGDBNAME (required): the name of the Postgres database to connect to.
  • PGHOST: the host of the Postgres cluster to connect to. Defaults to localhost.
  • PGPASSWORD (required): the password of the user to use when connecting to the Postgres database.
  • PGPORT: the port of the Postgres cluster to connect to. Defaults to 5432.
  • PGUSER (required): the user to use when connecting to the Postgres database
  • ALLOWED_STATEMENTS: CORS-like set of allowed statements in queries. Possible values are * for all statements (the default), an empty string for no allowed statements (effectively disabling the query interface), or a comma-separated list of allowed statements. Possible statements are SELECT, INSERT, UPDATE, and DELETE.
  • ALLOWED_FUNCTIONS: CORS-like set of function names that can be executed in queries. Possible values are * for all functions (the default), an empty string for no allowed functions, or a comma-separated list of allowed function names, e.g. to_json,pg_sleep, etc.

Usage

With PostgRPC running on the default port and host, grpcurl can be used to query the database:

grpcurl \
  -plaintext \
  -d '{"statement":"select 1 + 1 as two"}' \
  [::]:50051 postgres.Postgres/Query

# { "two": 2 }

To use a different (pre-existing) ROLE than the one used to connect to the database initially, use the X-Postgres-Role header:

grpcurl \
  -plaintext \
  -d '{"statement":"select current_user"}' \
  -H 'X-Postgres-Role: my-other-user' \
  [::]:50051 postgres.Postgres/Query

# { "current_user": "my-other-user" }

Examples

All examples can be run from the ./examples directory using docker-compose. Click on the links below to learn more about each example.

FAQ

  1. Who built PostgRPC? The team at Platter. Every Platter Postgres branching database comes with a gRPC and JSON interface much like this one.
  2. Is PostgRPC ready for production? If you're running this yourself, be sure to run it as a part of a stack that includes robust authentication and authorization, and ensure that you harden your Postgres database against malicious queries! If that sounds daunting, consider using Platter.
  3. How do you pronounce PostgRPC? "post-ger-puck"
You might also like...
Cyg will help you to secure files in your repository directly using PGP encryption

cyg: Secure files in your repository Cyg will help you to secure files in your repository directly using PGP encryption. The name "cyg" was inspired b

A JSON Query Language CLI tool built with Rust ๐Ÿฆ€

JQL A JSON Query Language CLI tool built with Rust ๐Ÿฆ€ ๐Ÿ“œ Core philosophy ๐Ÿ“ฆ Stay lightweight ๐ŸŽฎ Keep its features as simple as possible ๐Ÿง  Avoid redun

A small CLI tool to query ArcGIS REST API services, implemented in Rust. The server response is returned as pretty JSON.

A small CLI tool to query ArcGIS REST API services, implemented in Rust. The server response is returned as pretty JSON.

๐Ÿฆ€ Rust-based implementation of a Snowflake Generator which communicates using gRPC

Clawflake Clawflake is a Rust application which implements Twitter Snowflakes and communicates using gRPC. Snowflake ID numbers are 63 bits integers s

A generic Rust based Bigtable connection library implemented using gRPC

A generic Rust based Bigtable connection library refactored out the solana mono-repo so that can be shared for different applications.

CLI tool to work with Sled key-value databases.

sledtool CLI tool to work with Sled key-value databases. $ sledtool --help Usage: sledtool dbpath command [args] CLI tool to work with Sled da

Engula empowers engineers to build reliable and cost-effective databases.
Engula empowers engineers to build reliable and cost-effective databases.

Engula is a storage engine that empowers engineers to build reliable and cost-effective databases with less effort and more confidence. Engula is in t

A mini paste bin and url shortener written in rust without databases.

pb Build $ cargo build --release Environment Variables PB_DATA: /some/path (Default: ./pb_data) PB_SITE: Url of your site. (Default: http://localhost:

Sled - the champagne of beta embedded databases
Sled - the champagne of beta embedded databases

key value buy a coffee for us to convert into databases documentation chat about databases with us sled - it's all downhill from here!!! An embedded d

garbage-collecting on-disk object store, supporting higher level KV stores and databases.

marble Garbage-collecting disk-based object-store. See examples/kv.rs for a minimal key-value store built on top of this. Supports 4 methods: read: de

Optimistic multi-version concurrency control (MVCC) for main memory databases, written in Rust.

MVCC for Rust This is a work-in-progress the Hekaton optimistic multiversion concurrency control library in Rust. The aim of the project is to provide

This project provides a Rust-based solution for migrating MSSQL databases to MySQL.

MSSQL to MySQL Database Migration A Rust project to migrate MSSQL databases to MySQL, including table structures, column data types, and table data ro

Get JSON values quickly - JSON parser for Rust
Get JSON values quickly - JSON parser for Rust

get json values quickly GJSON is a Rust crate that provides a fast and simple way to get values from a json document. It has features such as one line

Schema2000 is a tool that parses exsiting JSON documents and tries to derive a JSON schema from these documents.

Schema 2000 Schema2000 is a tool that parses exsiting JSON documents and tries to derive a JSON schema from these documents. Currently, Schema2000 is

A stupid macro that compiles and executes Rust and spits the output directly into your Rust code

inline-rust This is a stupid macro inspired by inline-python that compiles and executes Rust and spits the output directly into your Rust code. There

SubZero - a standalone web server that turns your database directly into a REST/GraphQL api

What is this? This is a demo repository for the new subzero codebase implemented in Rust. subZero is a standalone web server that turns your database

Ask ChatGPT for a shell script, code, or anything, directly from your terminal ๐Ÿค–๐Ÿง ๐Ÿ‘จโ€๐Ÿ’ป
Ask ChatGPT for a shell script, code, or anything, directly from your terminal ๐Ÿค–๐Ÿง ๐Ÿ‘จโ€๐Ÿ’ป

ShellGPT Ask ChatGPT for a shell script, code, or anything, directly from your terminal ๐Ÿค– ๐Ÿง  ๐Ÿ‘จโ€๐Ÿ’ป Demo Install The binary is named gpt when installe

ask.sh: AI terminal assistant that can read and write your terminal directly!
ask.sh: AI terminal assistant that can read and write your terminal directly!

ask.sh: AI terminal assistant that read from & write to your terminal ask.sh is an AI terminal assistant based on OpenAI APIs such as GPT-3.5/4! What'

Play Hack The Box directly on your system.
Play Hack The Box directly on your system.

HTB Toolkit HTB Toolkit allows you to play Hack The Box machines directly on your system. Usage To use HTB Toolkit, you need to retrieve an App Token

Owner
Platter
Click once, build anything
Platter
๐Ÿ˜ Query your Postgres Database directly from the Browser.

Postgres Browser Proxy Query your Postgres Database directly from the Browser. Video: https://youtu.be/ohr9gBPC3cE Download the latest binaries: https

Yannick 6 Jan 7, 2023
reth-indexer reads directly from the reth db and indexes the data into a postgres database all decoded with a simple config file and no extra setup alongside exposing a API ready to query the data.

reth-indexer reth-indexer reads directly from the reth db and indexes the data into a postgres database all decoded with a simple config file and no e

Josh Stevens 306 Jul 12, 2023
Query is a Rust server for your remote SQLite databases and a CLI to manage them.

Query Query is a Rust server for your remote SQLite databases and a CLI to manage them. Table Of Contents Run A Query Server CLI Install Use The Insta

Vรญctor Garcรญa 6 Oct 6, 2023
Rust library and daemon for easily starting postgres databases per-test without Docker

pgtemp pgtemp is a Rust library and cli tool that allows you to easily create temporary PostgreSQL servers for testing without using Docker. The pgtem

Harry Stern 165 Mar 22, 2024
Provides a Rust-based SQLite extension for using Hypercore as the VFS for your databases.

SQLite and Hypercore A Rust library providing SQLite with an virtual file system to enable Hypercore as a means of storage. Contributing The primary r

Jacky Alcinรฉ 14 Dec 5, 2022
Grsql is a great tool to allow you set up your remote sqlite database as service and CRUD(create/read/update/delete) it using gRPC.

Grsql is a great tool to allow you set up your remote sqlite database as service and CRUD (create/ read/ update/ delete) it using gRPC. Why Create Thi

Bruce Yuan 33 Dec 16, 2022
JSON parser which picks up values directly without performing tokenization in Rust

Pikkr JSON parser which picks up values directly without performing tokenization in Rust Abstract Pikkr is a JSON parser which picks up values directl

Pikkr 615 Dec 29, 2022
An ether-rs middleware to access reth's db directly, bypassing JSON-RPC

Octane A ether-rs middleware for reth that bypasses JSON-RPC allowing for faster db queries. Work in Progress! Please note that Octane is currently in

Sorella Labs 75 Jun 4, 2023
Replibyte - a powerful tool to seed your databases

Seed Your Development Database With Real Data โšก๏ธ Replibyte is a powerful tool to seed your databases with real data and other cool features ?? Feature

Qovery 3.4k Jan 9, 2023
Save image from your clipboard ๐Ÿ“‹ as an image file directly from your command line! ๐Ÿ”ฅ

Clpy ?? Save copied image from clipboard as an image file directly from your command line! Note It works only on windows as of now. I'll be adding sup

Piyush Suthar 13 Nov 28, 2022