---
title: "Deploy Postgres Logs"
description: "Postgres with structured logs and correct severity levels"
category: "Storage"
url: https://railway.com/deploy/postgres-logs
---

# Deploy Postgres Logs

Postgres with structured logs and correct severity levels

**[Deploy Postgres Logs on Railway](https://railway.com/template/postgres-logs)**

- **Creator:** Andrey Maslov's Projects
- **Category:** Storage
- **Total deploys:** 2

## Template content

### Postgres Logs https://devicons.railway.app/i/postgresql.svg

- **Image:** ghcr.io/railwayapp-templates/postgres-ssl:17
- **Start command:** `bash -c 'log() { echo "[Relay] $1" > /proc/1/fd/1; }; log "Starting DB";wrapper.sh postgres -p 5432 -c listen_addresses=* -c log_destination=jsonlog -c logging_collector=on -c log_directory=log -c log_filename=postgresql.log -c log_rotation_age=0 -c log_rotation_size=0 -c log_truncate_on_rotation=off & pid=$!;LOG_FILE="$PGDATA/log/postgresql.json";log "Preparing pipe $LOG_FILE";if [ -p "$LOG_FILE" ]; then log "Pipe already exists";else while true; do if [ -f "$LOG_FILE" ]; then log "Replacing logfile with a pipe";mv -f "$LOG_FILE" "$LOG_FILE.prev";mkfifo "$LOG_FILE";chown postgres:postgres "$LOG_FILE";chmod 600 "$LOG_FILE";log "Pipe created";break;else log "Waiting for $LOG_FILE to exist...";sleep 1;fi;done;fi;log "Starting log streaming";cat "$LOG_FILE" | tee /tmp/pglog.raw | while IFS= read -r line;do line=${line//error_severity/level};printf "%s\n" "$line" > /proc/1/fd/1 || true;done & wait $pid'`

## Documentation

# Deploy and Host Postgres Logs on Railway

Postgres Logs is a Railway-ready template that deploys a fully managed PostgreSQL 17 instance with structured JSON logs automatically streamed to Railway’s Logs tab. It converts Postgres JSON logs into structured log lines with proper severity levels - so informational, warning, and error messages are clearly separated instead of all appearing as "error".

## About Hosting Postgres Logs

Hosting Postgres Logs on Railway means you get a persistent PostgreSQL database backed by a mounted volume and a clean TCP socket connection. This template automatically configures JSON logging, replaces the default file log with a named pipe, and relays output to stdout in a structured format. Railway’s log viewer then correctly recognizes each message level. The setup avoids false "error" classifications and provides real-time observability without external logging services, making debugging and monitoring smoother and more reliable.

## Common Use Cases

* Reliable log streaming from PostgreSQL to Railway’s Logs tab
* Structured JSON-based logging for better observability
* Easy debugging of queries, connections, and errors
* Building production-ready Postgres environments with correct log levels

## Dependencies for Postgres Logs Hosting

None - Postgres with Structured Logs comes ready to use with the standard configuration.  
Just copy `DATABASE_URL` variable value and set it to a service you want to connect.

### Deployment Dependencies

- Official Railway Postgres Docker Image version 17 - https://ghcr.io/railwayapp-templates/postgres-ssl:17

### Implementation Details

This template relies on a **Custom Start Command** that prepares and manages PostgreSQL logging in a way that’s compatible with Railway’s structured log parser.

1. **Initialization and Configuration**
   The command starts PostgreSQL via `wrapper.sh` with a set of configuration flags:

   * `log_destination=jsonlog` enables PostgreSQL’s built-in JSON log format.
   * `logging_collector=on` ensures that logs are written to a file.
   * Other flags like `log_rotation_age=0` and `log_truncate_on_rotation=off` disable rotation to keep the output consistent for piping.

   The server runs in the background while the script waits to prepare the logging pipeline.

2. **Log File Replacement with a FIFO Pipe**
   PostgreSQL writes its JSON logs into `$PGDATA/log/postgresql.json`.
   Once that file appears, the script immediately replaces it with a **named pipe (FIFO)**. This pipe allows logs to be continuously streamed rather than written to disk.

   * The pipe inherits proper permissions (`postgres:postgres`, `chmod 600`).
   * This ensures compatibility with PostgreSQL’s internal log writer process and prevents permission errors.

3. **Real-Time Log Streaming to Stdout**
   The script then uses `cat` to read the pipe and `tee` to duplicate output to a temporary raw log file (`/tmp/pglog.raw`) for debugging.
   Every incoming JSON line is processed in a small loop that replaces the field `"error_severity"` with `"level"`.
   This subtle transformation ensures that Railway’s internal log viewer correctly interprets each message’s severity (`INFO`, `WARNING`, `ERROR`, etc.) instead of treating all JSON logs as “error” events.

4. **Streaming into the Railway Log Collector**
   The final stream is written directly to `/proc/1/fd/1`, which represents the **stdout of the container’s init process** - this is what Railway uses for log ingestion.
   As a result, all PostgreSQL log entries appear in the **Logs tab** in real time, fully structured, without external log forwarders or sidecar containers.

5. **Graceful Shutdown and Sync**
   The main PostgreSQL process is tracked via its PID, and the script waits on it (`wait $pid`) to ensure proper cleanup and avoid premature container exit.

This design provides a minimal, elegant, and self-contained way to get structured, real-time PostgreSQL logs inside Railway - **no additional infrastructure, no false “error” spam, and full visibility into query-level events**.

## Why Deploy Postgres Logs on Railway?


Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying Postgres Logs on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.


## Similar templates

- [Garage S3 Storage](https://railway.com/deploy/garage-s3-storage) — Ultra-light S3 server: fast, open-source, plug-and-play.
- [Redis](https://railway.com/deploy/redis-1) — Self Host Latest Redis with Railway
- [EasyImg](https://railway.com/deploy/easyimg) — Simple self-hostable Nuxt.js personal image hosting system.

Open this page in a browser: https://railway.com/deploy/postgres-logs
