Deploy Postgres Logs

Postgres with structured logs and correct severity levels

Deploy Postgres Logs

Just deployed

/var/lib/postgresql/data

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

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.


Template Content

More templates in this category

View Template
Postgres-to-R2 Backup
Auto back up PostgreSQL databases to Cloudflare R2 with optional encryption

View Template
ReadySet
A lightweight caching engine for Postgres

View Template
Simple S3
Deploy a S3-compatible storage service with a pre-named bucket.