{"id":13344021,"url":"https://github.com/tonis2/zig-postgres","last_synced_at":"2025-03-12T06:31:04.999Z","repository":{"id":49899885,"uuid":"338810916","full_name":"tonis2/zig-postgres","owner":"tonis2","description":"Zig wrapper for postgres","archived":true,"fork":false,"pushed_at":"2021-10-18T10:04:39.000Z","size":150,"stargazers_count":29,"open_issues_count":1,"forks_count":9,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-08T04:43:44.923Z","etag":null,"topics":["binding","postgres","zig"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tonis2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-14T13:25:12.000Z","updated_at":"2024-01-07T19:03:23.000Z","dependencies_parsed_at":"2022-08-29T19:22:15.842Z","dependency_job_id":null,"html_url":"https://github.com/tonis2/zig-postgres","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonis2%2Fzig-postgres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonis2%2Fzig-postgres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonis2%2Fzig-postgres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonis2%2Fzig-postgres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tonis2","download_url":"https://codeload.github.com/tonis2/zig-postgres/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243171597,"owners_count":20247877,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binding","postgres","zig"],"created_at":"2024-07-29T19:32:19.615Z","updated_at":"2025-03-12T06:31:04.694Z","avatar_url":"https://github.com/tonis2.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zig-postgres\n\n\nLight bindings around Postgres `libpq`\n\nThis is tested with zig `0.8`\n\n\n\nInstalling `libpg` on debian linux\n\n\n`sudo apt-get install libpq-dev`\n\n\n\n## How to install \n\n-----\n\nAdd this repository as submodule \n\n`git submodule add git@github.com:tonis2/zig-postgres.git dependencies/zig-postgres`\n\n\nAdd following code lines into your project `build.zig`\n\n\nThis code adds the package and links required libraries.\n\n```zig\n    exe.addPackage(.{ .name = \"postgres\", .path = \"/dependencies/zig-postgres/src/postgres.zig\" });\n    exe.linkSystemLibrary(\"c\");\n    exe.linkSystemLibrary(\"libpq\");\n```\n\nRunning examples or tests requires `db` url attribute, for example \n\n`zig build test -Ddb=postgresql://db_url`\n\n`zig build main -Ddb=postgresql://db_url`\n\n## How to use\n-----\n\n\n\n\n### Connecting to database\n\n\n```zig\n    const Pg = @import(\"postgres\").Pg;\n\n    var gpa = std.heap.GeneralPurposeAllocator(.{}){};\n    const allocator = \u0026gpa.allocator;\n    defer std.debug.assert(!gpa.deinit());\n\n    var db = try Pg.connect(allocator, \"postgresql://root@postgresURL:26257?sslmode=disable\");\n\n```\n\n### Executing SQL\n\n```zig\n   const schema =\n        \\\\CREATE DATABASE IF NOT EXISTS root;\n        \\\\CREATE TABLE IF NOT EXISTS users (id INT, name TEXT, age INT);\n    ;\n\n    _ = try db.exec(schema);\n```\n\n\n### Inserting data\n\n\nBe mindful that this query, uses `struct name` as lowercase letters for `table` name.\n\n```zig\n  const Users = struct {\n        id: i16,\n        name: []const u8,\n        age: i16,\n    };\n\n _ = try db.insert(Users{ .id = 1, .name = \"Charlie\", .age = 20 });\n _ = try db.insert(Users{ .id = 2, .name = \"Steve\", .age = 25 });\n _ = try db.insert(Users{ .id = 3, .name = \"Karl\", .age = 25 });\n\n\n _ = try db.insert(\u0026[_]Users{\n     Users{ .id = 4, .name = \"Tony\", .age = 25 },\n     Users{ .id = 5, .name = \"Sara\", .age = 32 },\n     Users{ .id = 6, .name = \"Fred\", .age = 11 },\n  });\n\n```\n\n\n### Exec query with values\n\n```zig\n_ = try db.execValues(\"SELECT * FROM users WHERE name = {s}\", .{\"Charlie\"});\n\n_ = try db.execValues(\"INSERT INTO users (id, name, age) VALUES ({d}, {s}, {d})\", .{ 5, \"Tom\", 32 });\n\n```\n\n\n### Read query results\n\n```zig\nvar result = try db.execValues(\"SELECT * FROM users WHERE id = {d}\", .{2});\nvar user = result.parse(Users, null).?;\n\nprint(\"{d} \\n\", .{user.id});\nprint(\"{s} \\n\", .{user.name});\n\n```\n\n\n```zig\nvar results = try db.execValues(\"SELECT * FROM users WHERE age = {d}\", .{25});\n\nwhile (results.parse(Users, null)) |user| {\n    print(\"{s} \\n\", .{user.name});\n}\n```\n\n```zig\nvar result = try db.execValues(\"SELECT * FROM users WHERE name = {s}\", .{\"Charlie\"});\nvar user = result.parse(Users, null}).?;\n\nif(user) print(\"{s} \\n\", .{user.name});\n```\n\n\nMany thanks for this [repository](https://github.com/aeronavery/zig-orm) \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftonis2%2Fzig-postgres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftonis2%2Fzig-postgres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftonis2%2Fzig-postgres/lists"}