<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/using-rust-and-postgres-for-everything-patterns-learned-over-the-years-f5lynsm5x" -->

---
title: Using Rust and Postgres for everything: patterns learned...
description: A practitioner shares battle-tested patterns for building backend services with Rust and PostgreSQL, covering: using sqlx over ORMs for type-safe SQL with...
canonical: https://daily.dev/posts/using-rust-and-postgres-for-everything-patterns-learned-over-the-years-f5lynsm5x
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Using Rust and Postgres for everything: patterns learned over the years | daily.dev
og:description: A practitioner shares battle-tested patterns for building backend services with Rust and PostgreSQL, covering: using sqlx over ORMs for type-safe SQL with...
og:url: https://daily.dev/posts/using-rust-and-postgres-for-everything-patterns-learned-over-the-years-f5lynsm5x
og:image: https://api.daily.dev/og/posts/f5Lynsm5X.png
og:image:alt: Using Rust and Postgres for everything: patterns learned over the years
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Using Rust and Postgres for everything: patterns learned over the years

**[Sylvain Kerkour](https://daily.dev/sources/kerkour)** · 7 min read · 0 upvotes · 0 comments

## Summary

A practitioner shares battle-tested patterns for building backend services with Rust and PostgreSQL, covering: using sqlx over ORMs for type-safe SQL with compile-time query validation, batching inserts/updates via UNNEST with sqlx 0.9+ iterator binding, implementing leader election with PostgreSQL advisory locks, replacing Redis with PostgreSQL UNLOGGED tables for ephemeral data, and using PostgreSQL as a job queue with UUID v7 IDs and FOR UPDATE SKIP LOCKED. A real-world rewrite from Go to Rust is cited: batch jobs dropped from ~30 minutes to under 5 minutes, and RAM usage fell from 4 GB to 512 MB.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://kerkour.com/rust-postgres-everything>

## Questions this post answers

### How do I batch insert multiple rows in Rust using sqlx with PostgreSQL?

With sqlx 0.9+, you can bind iterators directly using `.bind_iter()` and pass arrays to PostgreSQL via UNNEST. The pattern is: build a query using `INSERT INTO ... SELECT * FROM UNNEST($1::UUID[], $2::TEXT[])`, then bind each column as an iterator over your slice. Batches should be capped at around 10,000 rows to avoid overloading the database.

_Rust developers handling bulk writes track sqlx patterns and releases on daily.dev._

### How do I implement leader election in a horizontally scaled Rust backend using PostgreSQL?

Use PostgreSQL advisory locks via `pg_try_advisory_lock($1)`. Acquire a connection from the pool, call the lock function with a fixed integer ID, and if it returns true the instance is leader. The lock is held as long as the connection is open — dropping the connection releases it and allows another instance to become leader. Poll in a loop with a sleep interval until the lock is acquired.

_Teams running stateless, horizontally scaled Rust services share architecture decisions like this on daily.dev._

### Can I use PostgreSQL as a job queue instead of Redis or a dedicated queue service?

PostgreSQL works well as a job queue when you use UUID v7 as job IDs (to avoid index fragmentation) and `FOR UPDATE SKIP LOCKED` in the pull query to prevent multiple workers from claiming the same job. A status column (Queued/Running/Failed) and a `failed_attempts` counter handle retries. This eliminates the need for a separate stateful service like Redis or RabbitMQ.

_Developers simplifying their stack by replacing Redis with PostgreSQL find real-world trade-offs discussed on daily.dev._

## Similar posts on daily.dev

- [Building scalable backend services with Rust and PostgreSQL](https://daily.dev/posts/building-scalable-backend-services-with-rust-and-postgresql-tlwj69w1v) · Sylvain Kerkour · 6 upvotes · 3 comments
- [Rebuilding Postgres for 300x faster analytics: batching, operator fusion, and SIMD](https://daily.dev/posts/rebuilding-postgres-for-300x-faster-analytics-batching-operator-fusion-and-simd-tgtpnmlyi) · Hacker News · 0 upvotes · 0 comments
- [Why I Chose Rust for 0xInsider](https://daily.dev/posts/why-i-chose-rust-for-0xinsider-bzitxczjz) · Trevor Lasn · 21 upvotes · 6 comments

---

Tags: [#backend](https://daily.dev/tags/backend), [#rust](https://daily.dev/tags/rust), [#postgresql](https://daily.dev/tags/postgresql)

[View this post on daily.dev](https://daily.dev/posts/using-rust-and-postgres-for-everything-patterns-learned-over-the-years-f5lynsm5x)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Using Rust and Postgres for everything: patterns learned over the years","url":"https://daily.dev/posts/using-rust-and-postgres-for-everything-patterns-learned-over-the-years-f5lynsm5x","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/using-rust-and-postgres-for-everything-patterns-learned-over-the-years-f5lynsm5x"},"datePublished":"2026-08-11T08:53:56.239Z","dateModified":"2026-08-11T09:18:04.938Z","description":"A practitioner shares battle-tested patterns for building backend services with Rust and PostgreSQL, covering: using sqlx over ORMs for type-safe SQL with...","image":"https://media.daily.dev/image/upload/s--qPvKM23u--/f_auto/v1722860399/public/Placeholder%2009","thumbnailUrl":"https://media.daily.dev/image/upload/s--qPvKM23u--/f_auto/v1722860399/public/Placeholder%2009","isAccessibleForFree":true,"articleSection":"Sylvain Kerkour","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Sylvain Kerkour","logo":"https://media.daily.dev/image/upload/s--3ngWt4Yp--/f_auto,q_auto/v1786438308/logos/kerkour?_a=BAMAMicg0","url":"https://daily.dev/sources/kerkour"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/using-rust-and-postgres-for-everything-patterns-learned-over-the-years-f5lynsm5x","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"backend,rust,postgresql","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Sylvain Kerkour","item":"https://daily.dev/sources/kerkour"},{"@type":"ListItem","position":3,"name":"Using Rust and Postgres for everything: patterns learned over the years"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/using-rust-and-postgres-for-everything-patterns-learned-over-the-years-f5lynsm5x#faq","mainEntity":[{"@type":"Question","name":"How do I batch insert multiple rows in Rust using sqlx with PostgreSQL?","acceptedAnswer":{"@type":"Answer","text":"With sqlx 0.9+, you can bind iterators directly using `.bind_iter()` and pass arrays to PostgreSQL via UNNEST. The pattern is: build a query using `INSERT INTO ... SELECT * FROM UNNEST($1::UUID[], $2::TEXT[])`, then bind each column as an iterator over your slice. Batches should be capped at around 10,000 rows to avoid overloading the database. Rust developers handling bulk writes track sqlx patterns and releases on daily.dev."}},{"@type":"Question","name":"How do I implement leader election in a horizontally scaled Rust backend using PostgreSQL?","acceptedAnswer":{"@type":"Answer","text":"Use PostgreSQL advisory locks via `pg_try_advisory_lock($1)`. Acquire a connection from the pool, call the lock function with a fixed integer ID, and if it returns true the instance is leader. The lock is held as long as the connection is open — dropping the connection releases it and allows another instance to become leader. Poll in a loop with a sleep interval until the lock is acquired. Teams running stateless, horizontally scaled Rust services share architecture decisions like this on daily.dev."}},{"@type":"Question","name":"Can I use PostgreSQL as a job queue instead of Redis or a dedicated queue service?","acceptedAnswer":{"@type":"Answer","text":"PostgreSQL works well as a job queue when you use UUID v7 as job IDs (to avoid index fragmentation) and `FOR UPDATE SKIP LOCKED` in the pull query to prevent multiple workers from claiming the same job. A status column (Queued/Running/Failed) and a `failed_attempts` counter handle retries. This eliminates the need for a separate stateful service like Redis or RabbitMQ. Developers simplifying their stack by replacing Redis with PostgreSQL find real-world trade-offs discussed on daily.dev."}}]}
```

