---
title: "Deploy PostgreSQL Server"
description: "Production-ready PostgreSQL with persistent storage and TCP access."
category: "Storage"
url: https://railway.com/deploy/postgresql-server
---

# Deploy PostgreSQL Server

Production-ready PostgreSQL with persistent storage and TCP access.

**[Deploy PostgreSQL Server on Railway](https://railway.com/template/postgresql-server)**

- **Creator:** INF Labs
- **Category:** Storage

## Template content

### postgres https://devicons.railway.com/i/postgresql.svg

- **Image:** postgres:latest

## Documentation

# Deploy and Host PostgreSQL Server on Railway

PostgreSQL is a powerful open-source relational database designed for reliable transactional workloads, APIs, backend services, analytics, and modern application development. It supports SQL, ACID transactions, JSON, indexing, extensions, replication, and a mature ecosystem used across small applications and large production systems.

## About Hosting PostgreSQL Server

Hosting PostgreSQL on Railway gives you a persistent relational database without manually managing a virtual machine, operating system, database package installation, or server lifecycle.

This template runs PostgreSQL as a dedicated database service with persistent Railway storage. Applications inside the same Railway project can connect through Railway private networking, while external clients can connect through Railway TCP Proxy.

The template also provides ready-to-use connection variables for both internal and external access, making it easy to connect applications, migration tools, database clients, and backend services.

## Common Use Cases

* Backend database for web and mobile applications
* API and microservice data storage
* SaaS application databases
* Transactional business systems
* JSON and relational workloads
* Analytics and reporting backends
* Development and testing environments
* Data storage for Django, FastAPI, Node.js, Rails, Laravel, and other frameworks

## Dependencies for PostgreSQL Hosting

* Official PostgreSQL Docker image
* Persistent Railway volume mounted at `/var/lib/postgresql/data`
* Railway private networking for internal connections
* Optional Railway TCP Proxy for external database access

### Implementation Details

PostgreSQL communicates using its native wire protocol on:

```text
TCP 5432
```

Database files are persisted inside the Railway volume at:

```text
/var/lib/postgresql/data
```

A dedicated PostgreSQL data directory is used inside the mounted volume so database state remains available across redeployments and container restarts.

## PostgreSQL Architecture on Railway

```text
Application / Backend
        │
        │ Railway Private Network
        ▼
 PostgreSQL :5432
        │
        ▼
 Railway Volume
 /var/lib/postgresql/data
```

External database clients can connect through Railway TCP Proxy:

```text
DBeaver / pgAdmin / psql
        │
        │ TCP Proxy
        ▼
 PostgreSQL :5432
```

PostgreSQL is not an HTTP service, so a regular Railway public domain is not used for database connections.

## Database Connection

### Applications Inside Railway

Applications deployed inside the same Railway project should use the private database connection URL.

This keeps database traffic inside Railway's private network and avoids unnecessary public exposure.

Typical usage:

```text
${{PostgreSQL-Server.DATABASE_URL}}
```

This connection uses the Railway private hostname and internal PostgreSQL port `5432`.

### External Applications and Database Clients

For connections from outside Railway, use:

```text
${{PostgreSQL-Server.DATABASE_PUBLIC_URL}}
```

This URL uses Railway TCP Proxy.

External tools may include:

* DBeaver
* pgAdmin
* DataGrip
* TablePlus
* `psql`
* migration tools
* local development environments

## Connection Details

A PostgreSQL connection typically contains:

```text
Host
Port
Database
Username
Password
```

Internal Railway connections use port:

```text
5432
```

External connections use the hostname and dynamically assigned port provided by Railway TCP Proxy.

Example URI structure:

```text
postgresql://USERNAME:PASSWORD@HOST:PORT/DATABASE
```

## Testing PostgreSQL

After connecting, verify the server version:

```sql
SELECT version();
```

Check the current database:

```sql
SELECT current_database();
```

Create a simple table:

```sql
CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    created_at TIMESTAMPTZ DEFAULT NOW()
);
```

Insert sample data:

```sql
INSERT INTO users (name, email)
VALUES ('Example User', 'user@example.com');
```

Query the table:

```sql
SELECT *
FROM users;
```

## PostgreSQL Features

| Capability                      | PostgreSQL |
| ------------------------------- | :--------: |
| Open-source                     |      ✅     |
| ACID transactions               |      ✅     |
| SQL support                     |      ✅     |
| JSON / JSONB                    |      ✅     |
| Full-text search                |      ✅     |
| Advanced indexing               |      ✅     |
| Views and materialized views    |      ✅     |
| Stored procedures and functions |      ✅     |
| Extensions                      |      ✅     |
| Replication support             |      ✅     |
| Role-based database permissions |      ✅     |
| Persistent Railway storage      |      ✅     |
| Private Railway networking      |      ✅     |
| External TCP access             |      ✅     |

PostgreSQL combines traditional relational database features with document-style JSON capabilities, advanced indexing, custom data types, and an extensive extension ecosystem.

## PostgreSQL vs Similar Relational Databases

| Feature                       | PostgreSQL | MySQL | MariaDB | SQLite |
| ----------------------------- | :--------: | :---: | :-----: | :----: |
| Open-source                   |      ✅     |   ✅   |    ✅    |    ✅   |
| Relational SQL                |      ✅     |   ✅   |    ✅    |    ✅   |
| ACID transactions             |      ✅     |   ✅   |    ✅    |    ✅   |
| Native JSON support           |      ✅     |   ✅   |    ✅    |    ✅   |
| Advanced JSON querying        |      ✅     |   ❌   |    ❌    |    ❌   |
| Extension ecosystem           |      ✅     |   ❌   |    ❌    |    ❌   |
| Full-text search              |      ✅     |   ✅   |    ✅    |    ✅   |
| Multi-user database server    |      ✅     |   ✅   |    ✅    |    ❌   |
| Advanced indexing options     |      ✅     |   ✅   |    ✅    |    ❌   |
| Stored procedures             |      ✅     |   ✅   |    ✅    |    ❌   |
| Embedded single-file database |      ❌     |   ❌   |    ❌    |    ✅   |

PostgreSQL is particularly useful for applications that require strong transactional guarantees, complex queries, relational modeling, JSON workloads, extensibility, and advanced indexing in a single database system.

MySQL and MariaDB remain strong choices for MySQL-compatible applications, while SQLite is better suited for embedded and lightweight single-process workloads.

## Persistent Storage

A Railway volume keeps PostgreSQL data persistent across deployments.

Persistent data includes:

* databases
* tables
* indexes
* users and roles
* sequences
* schemas
* transaction state
* extension metadata

Without persistent storage, database data would be lost when the container is replaced.

## Security and Access

PostgreSQL initializes with generated credentials stored in the Railway service variables.

For application workloads:

* use private networking whenever possible
* avoid exposing the database publicly unless required
* use TCP Proxy only for external administration or external applications
* create dedicated application roles when different privilege levels are needed
* avoid using the PostgreSQL administrator account for every application

## Getting Started After Deployment

1. Deploy the PostgreSQL Server template.
2. Wait for the database service to become available.
3. Use `DATABASE_URL` for applications running inside the same Railway project.
4. Use `DATABASE_PUBLIC_URL` when connecting from outside Railway.
5. Connect using `psql`, DBeaver, pgAdmin, DataGrip, or your application.
6. Verify the database with:

```sql
SELECT version();
```

7. Create your schemas, tables, indexes, roles, and application data as required.

## Why Deploy PostgreSQL Server 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 PostgreSQL Server on Railway, you get persistent storage, private networking, optional external TCP access, and ready-to-use connection details in a deployment that can be integrated directly with your applications and backend services.


## 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/postgresql-server
