Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/_assets/icon/zig-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions docs/connect/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an

::::

::::{grid-item-card} Zig
:link: connect-zig
:link-type: ref
:link-alt: Connect to CrateDB using Zig
:padding: 3
:text-align: center
:class-card: sd-pt-3
:class-body: sd-fs-1
:class-title: sd-fs-6
```{image} /_assets/icon/zig-logo.png
:height: 50px
```
::::

:::::


Expand Down Expand Up @@ -187,6 +201,7 @@ javascript
php
python
ruby
zig/index
natural
All drivers <drivers>
```
Expand Down
90 changes: 90 additions & 0 deletions docs/connect/zig/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
(connect-zig)=

# Zig

:::{div} sd-text-muted
Connect to CrateDB from Zig applications.
:::

:::{rubric} About
:::

[pg.zig] is a native PostgreSQL driver / client for Zig.

:::{rubric} Synopsis
:::

`build.zig`
```zig
const std = @import("std");

pub fn build(b: *std.Build) void {

const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const exe = b.addExecutable(.{
.name = "example",
.root_module = b.createModule(.{
.root_source_file = b.path("example.zig"),
.target = b.graph.host,
})
});

const pg = b.dependency("pg", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("pg", pg.module("pg"));

b.installArtifact(exe);
}
```
`example.zig`
```zig
const std = @import("std");
const builtin = @import("builtin");

const pg = @import("pg");

pub fn main() !void {

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = if (builtin.mode == .Debug) gpa.allocator() else std.heap.c_allocator;

const uri = try std.Uri.parse("postgresql://crate:crate@localhost/doc?sslmode=disable");
var pool = try pg.Pool.initUri(allocator, uri, .{.size=5, .timeout=5_000});
defer pool.deinit();

var result = try pool.query("SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3", .{});
defer result.deinit();

while (try result.next()) |row| {
const mountain = row.get([]u8, 0);
const height = row.get(i32, 1);
std.debug.print("{s}: {d}\n", .{mountain, height});
}

}
```

:::{include} ../_cratedb.md
:::
```shell
zig fetch --save git+https://github.com/karlseguin/pg.zig#master
zig build
./zig-out/bin/example
```
Comment on lines +73 to +77
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about zig run? Maybe revisit on a future iteration?

Copy link
Member Author

@amotl amotl Oct 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like zig run isn't able to pick up the dependencies properly. Maybe we need to extend the library search path of any kind?

$ zig run example.zig
example.zig:4:20: error: no module named 'pg' available within module 'example'
const pg = @import("pg");
                   ^~~~


:::{rubric} CrateDB Cloud
:::

For connecting to CrateDB Cloud, use the `sslmode=require` parameter,
and replace username, password, and hostname with values matching your
environment.
```zig
const uri = try std.Uri.parse("postgresql://admin:password@testcluster.cratedb.net:5432/doc?sslmode=require");
```


[pg.zig]: https://github.com/karlseguin/pg.zig