NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / RASTERIZEDDB SERVER / CONNECTING

Connecting

This page shows how clients reach the server: which protocol listens where, how TLS and unix sockets work, connection strings for psql and common drivers, and how a connection picks its database. Every flag named here is listed in Configuration.

Protocols and ports

ProtocolDefault addressTurned on byClients
PostgreSQL wire protocol127.0.0.1:5432--protocol pgwire or --protocol bothpsql, libpq, JDBC, Npgsql, sqlx, tokio-postgres, node-postgres, psycopg, ORMs
Native protocol (rastcp)127.0.0.1:61170--protocol rastcp (the default) or --protocol bothrasterizeddb_client, the .NET client, Studio
MySQL / MariaDBoff--mysql-port <PORT> or --mysql-socket <PATH>mysql, mariadb, MySQL drivers
SQL Server (TDS)off--mssql-port <PORT> or --mssql-socket <PATH>sqlcmd, Microsoft.Data.SqlClient, tiberius
Redis (RESP)off--kv-port <PORT> or --kv-socket <PATH>redis-cli, Redis clients

All of them serve the same databases and check logins against the same roles. Every listener binds 127.0.0.1 by default; give its address flag (--pg-addr, --rdb-addr, --mysql-addr, --mssql-addr, --kv-addr) the value 0.0.0.0 to accept other machines.

The default --protocol is rastcp, the native protocol alone. If psql reports "connection refused" on port 5432, start the server with --protocol both.

A typical server for both PostgreSQL clients and native clients, reachable from the network:

sh
export RASTERIZEDDB_PG_PASSWORD='choose-a-strong-password'
rasterizeddb --location /var/lib/rasterizeddb --protocol both \
  --pg-addr 0.0.0.0 --rdb-addr 0.0.0.0

The message broker adds its own ports (--broker-port, --kafka-port, --amqp-port, --mqtt-port), all off unless given. See Message broker.

PostgreSQL protocol

TLS

--pg-tls decides what the PostgreSQL listener accepts over TCP:

--pg-tlsCleartext connectionEncrypted connectionClient sslmode that works
require (default)refused with SQLSTATE 08004acceptedrequire
preferacceptedaccepteddisable, prefer, require
disableacceptedrefuseddisable, prefer

With no --pg-cert/--pg-key, the server makes a new self-signed certificate at every start. A client can encrypt to it but cannot verify it, so sslmode=require (encrypt, do not verify) works and verify-ca/verify-full do not. To let clients verify the server, give it a certificate your clients trust:

sh
rasterizeddb --location /var/lib/rasterizeddb --protocol both --pg-addr 0.0.0.0 \
  --pg-cert /etc/rasterizeddb/server.crt --pg-key /etc/rasterizeddb/server.key

Both files are PEM; the certificate file may hold the whole chain. The server warns at --log-level warn when the PostgreSQL listener is on a non-loopback address without --pg-tls require, since the login exchange and every row would then cross the network in the clear.

Authentication

--pg-auth sets how logins are checked: scram-sha-256 (the default), md5, password (cleartext, only safe under TLS) or trust (no check). See Security.

Connection strings

The examples use the default role rasterized, the default database postgres, and the default TLS setting with the self-signed certificate.

psql and libpq:

sh
psql "postgresql://rasterized@db.example.com:5432/postgres?sslmode=require"
psql "host=db.example.com port=5432 user=rasterized dbname=postgres sslmode=require"

Put the password in PGPASSWORD or ~/.pgpass rather than in the URL. With libpq, sslmode=require checks the certificate anyway if a root certificate file (~/.postgresql/root.crt) exists; move it aside, or give the server a certificate signed by that root.

JDBC (pgjdbc):

text
jdbc:postgresql://db.example.com:5432/postgres?user=rasterized&password=secret&sslmode=require

Npgsql (.NET):

text
Host=db.example.com;Port=5432;Database=postgres;Username=rasterized;Password=secret;SSL Mode=Require;Trust Server Certificate=true

Trust Server Certificate=true makes Npgsql versions before 8 accept the self-signed certificate. From Npgsql 8, SSL Mode=Require encrypts without verifying and the option is no longer needed.

sqlx (Rust): enable a TLS feature of sqlx (for example tls-rustls), then:

rust
use sqlx::postgres::PgPoolOptions;

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect("postgres://rasterized:secret@db.example.com:5432/postgres?sslmode=require")
        .await?;
    let row: (i64,) = sqlx::query_as("SELECT 1::BIGINT").fetch_one(&pool).await?;
    assert_eq!(row.0, 1);
    Ok(())
}

tokio-postgres (Rust): tokio-postgres verifies certificates through whichever TLS connector you give it. On one machine, the simplest is a server started with --pg-tls prefer and a cleartext loopback connection:

rust
use tokio_postgres::NoTls;

#[tokio::main]
async fn main() -> Result<(), tokio_postgres::Error> {
    let (client, connection) = tokio_postgres::connect(
        "host=127.0.0.1 port=5432 user=rasterized password=secret dbname=postgres sslmode=disable",
        NoTls,
    )
    .await?;
    tokio::spawn(connection);
    let row = client.query_one("SELECT 1::BIGINT", &[]).await?;
    let one: i64 = row.get(0);
    assert_eq!(one, 1);
    Ok(())
}

Across a network, give the server a real certificate and use a verifying connector (tokio-postgres-rustls or postgres-native-tls).

node-postgres:

javascript
const { Client } = require("pg");
const client = new Client({
  host: "db.example.com",
  port: 5432,
  user: "rasterized",
  password: "secret",
  database: "postgres",
  ssl: { rejectUnauthorized: false },
});

(async () => {
  await client.connect();
  const result = await client.query("SELECT 1 AS one");
  console.log(result.rows[0].one);
  await client.end();
})();

rejectUnauthorized: false accepts the self-signed certificate. With a real certificate, pass its CA instead: ssl: { ca: fs.readFileSync("ca.pem") }.

psycopg (Python):

python
import psycopg
with psycopg.connect("host=db.example.com port=5432 user=rasterized password=secret dbname=postgres sslmode=require") as conn:
    print(conn.execute("SELECT 1").fetchone())

Startup parameters

The server honours statement_timeout and lock_timeout sent at connect time, on their own or inside options:

sh
psql "host=127.0.0.1 user=rasterized dbname=postgres sslmode=require options='-c statement_timeout=5s'"

Other parameters a driver sends at startup (application_name, client_encoding, DateStyle) are accepted. The server reports itself as PostgreSQL 16.3 (server_version) with UTF8 encoding and the UTC time zone.

Cancelling a statement

PostgreSQL cancel requests are supported: Ctrl+C in psql, Statement.cancel() in JDBC, NpgsqlCommand.Cancel(), and the cancel token of other drivers stop the running statement.

Connection limits and timeouts

FlagDefaultEffect
--max-connections500One more session is refused with SQLSTATE 53300, "sorry, too many clients already".
--authentication-timeout60sA client that has not finished TLS and login by then is closed.
--idle-in-transaction-session-timeoutoffEnds sessions idle inside a transaction (25P03).
--idle-session-timeoutoffEnds idle sessions (57P05).

Use a connection pool in the application, or PgBouncer in front of the server, when you need more clients than --max-connections.

Native protocol

The native protocol is what rasterizeddb_client (Rust), the .NET client and Studio use. Over TCP it is always TLS 1.3, and logins use SCRAM-SHA-256 bound to the TLS connection, so a password never crosses the wire and an intercepted exchange cannot be replayed onto another connection.

rust
use rasterizeddb_client::{Client, Login, Options};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::connect(
        "db.example.com:61170",
        Options {
            login: Login::password("rasterized", "secret"),
            database: Some("postgres".into()),
            ..Options::default()
        },
    )
    .await?;
    let rows = client.query("SELECT 1", &[]).await?;
    println!("{} row", rows.len());
    Ok(())
}

See Rust client: connecting for every option, including verifying the server's certificate.

FlagDefaultEffect
--rdb-addr127.0.0.1A non-loopback address is refused at start unless a password is set (--rdb-password, RASTERIZEDDB_PASSWORD, or a role with one).
--rdb-port61170
--rdb-authscram-sha-256scram-sha-256 or trust. md5 and cleartext are not offered.
--rdb-cert, --rdb-keyself-signed at every startA certificate clients can verify.
--rdb-request-timeout30 secondsLongest one statement may run (57014); also closes connections idle that long.

Under --protocol both the native protocol accepts the same role and password as the PostgreSQL side. Keep --pg-auth scram-sha-256 then: the native protocol answers only SCRAM, so a role whose password was stored for md5 cannot log in over it.

Unix sockets

On Linux every listener can also serve a unix socket for clients on the same machine, and its TCP port can be turned off. A socket has no TLS; who may connect is decided by the socket file's permissions.

ListenerSocket flagTCP offSocket authentication
PostgreSQL--pg-socket-dir DIR (creates DIR/.s.PGSQL.<pg-port>)--pg-tcp off--pg-socket-auth, defaults to --pg-auth
Native--rdb-socket PATH--rdb-tcp off--rdb-socket-auth, defaults to --rdb-auth
MySQL--mysql-socket PATHleave out --mysql-port--mysql-socket-auth
SQL Server--mssql-socket PATHleave out --mssql-port--mssql-socket-auth
Redis--kv-socket PATHleave out --kv-port--kv-auth
sh
rasterizeddb --location /var/lib/rasterizeddb --protocol both \
  --pg-socket-dir /run/rasterizeddb --pg-socket-auth peer \
  --rdb-socket /run/rasterizeddb/native.sock \
  --socket-mode 660 --socket-group app

Connect over the PostgreSQL socket by giving the directory as the host:

sh
psql "host=/run/rasterizeddb port=5432 user=$USER dbname=postgres"
  • --socket-mode (octal, default 600) and --socket-group apply to every socket. Connecting needs write permission, so 660 with a group admits that group's members, which is how a service running as another user gets access.
  • peer authentication, offered on sockets only, logs a role in without a password when its name is the operating-system user connecting. The role must exist and be allowed to log in.
  • trust is allowed on a socket only when its mode admits nobody outside its owner and group.
  • A socket file left behind by a server that did not shut down is replaced at start. One another server is listening on is refused.
  • A socket path must fit in about 107 bytes on Linux.
  • Under --pg-socket-dir a TLS request is answered "no", whatever --pg-tls says, so --pg-tls require applies to TCP only.

MySQL and SQL Server clients

sh
rasterizeddb --location /var/lib/rasterizeddb --protocol both \
  --mysql-port 3306 --mssql-port 1433

mysql -h 127.0.0.1 -P 3306 -u rasterized -p postgres
sqlcmd -S 127.0.0.1,1433 -U rasterized -P 'secret' -d postgres -C

MySQL clients log in with caching_sha2_password; a role's first login over TCP needs TLS, which the mysql client uses by default. SQL Server clients use SQL Server authentication (Windows authentication is not offered); -C makes sqlcmd accept the self-signed certificate. Both listeners use --pg-cert/--pg-key unless given their own. What each dialect supports is on MySQL and SQL Server clients.

Redis clients

sh
rasterizeddb --location /var/lib/rasterizeddb --protocol both --kv-port 6379
redis-cli -p 6379 --user rasterized --pass 'secret' PING

AUTH <password> without a user name is checked as --kv-user (default --pg-user). See Key/value cache.

Selecting a database

Each connection works in one database, chosen when it connects. A name that does not exist is refused with SQLSTATE 3D000, database "<name>" does not exist; the server never silently gives you a different database.

ClientHow to name the database
libpq URLthe path: postgresql://host/analytics
libpq keywordsdbname=analytics
JDBCthe path: jdbc:postgresql://host:5432/analytics
NpgsqlDatabase=analytics
rasterizeddb_clientOptions::database
MySQLthe database argument, or USE analytics
SQL Server-d analytics, or Database= in the connection string

A connection that names none gets --pg-default-db (postgres by default), the database stored in the data directory itself. Roles are per database: a login is checked against the roles of the database the connection names. See Databases.

Limits

  • Unix sockets and peer authentication are Linux-only.
  • The self-signed certificate changes at every start, so it cannot be pinned. Use --pg-cert/--rdb-cert for clients that verify.
  • --max-connections has no unlimited setting.
  • A transaction cannot span two databases; open one connection per database.
✓ page rendered in 202 µs axum + tera + htmx · no other javascript