Deploy Astro + PostgreSQL

Blazing fast Astro with PostgreSQL database - zero config deployment

Deploy Astro + PostgreSQL

/var/lib/postgresql/data

Just deployed

Deploy and Host Astro + PostgreSQL on Railway

A lightning-fast Astro template with PostgreSQL database for building modern, database-driven websites. Perfect for blogs, documentation sites, portfolios, and content-heavy applications that need the speed of static sites with dynamic database content. Zero configuration required - deploy and start building immediately.

About Hosting Astro + PostgreSQL

This template deploys a complete Astro application with PostgreSQL database integration. Railway handles the Docker build process that compiles your Astro site, provisions a PostgreSQL database, and runs database migrations automatically to create tables and seed initial data. Astro's server-side rendering generates HTML on-demand from database queries, delivering fast page loads with zero JavaScript by default. The blog system demonstrates dynamic content from PostgreSQL with author attribution, publication dates, hero images, and unique slugs for SEO-friendly URLs. Health checks are pre-configured at / for Railway monitoring. Public networking is enabled with automatic SSL certificates. The entire stack deploys in under 2 minutes with production-ready configuration.

Common Use Cases

  • Blogs and publications - SEO-friendly content management with database-driven posts, categories, and tags
  • Documentation sites - Technical docs with searchable database content, versioning, and navigation
  • Portfolio websites - Showcase projects with dynamic updates, case studies, and testimonials from database
  • Landing pages - Marketing sites with database-driven features, testimonials, and A/B testing content
  • News and media sites - Article management with authors, publication dates, and content categorization
  • Product catalogs - E-commerce product pages from database with descriptions, images, and specifications
  • Company websites - Corporate sites with team members, job postings, and press releases from database

Dependencies for Astro + PostgreSQL Hosting

  • Node.js 20+ - Provided via node:20-alpine Docker image for build and runtime
  • Astro - Ultra-fast static site framework with server-side rendering capabilities
  • PostgreSQL - Automatically provisioned by Railway with DATABASE_PUBLIC_URL environment variable
  • Postgres.js - Fast PostgreSQL client for Node.js (postgres npm package)
  • Tailwind CSS - Utility-first CSS framework for responsive styling
  • TypeScript - Type-safe development with full IDE support

Deployment Dependencies

Implementation Details

Astro Server-Side Rendering: Dynamic content from PostgreSQL with zero JavaScript by default:

---
import { sql } from '../lib/db';

const posts = await sql`
  SELECT * FROM posts 
  ORDER BY pub_date DESC 
  LIMIT 10
`;
---

<div class="grid gap-8">
  {posts.map(post =&gt; (
    <article>
      <h2>{post.title}</h2>
      <p>{post.description}</p>
      <a href="{`/blog/${post.slug}`}">Read more</a>
    </article>
  ))}
</div>

Dynamic Routes with Database: Create pages from database content:

---
// src/pages/blog/[slug].astro
const { slug } = Astro.params;
const posts = await sql`SELECT * FROM posts WHERE slug = ${slug}`;
const post = posts[0];
---

<article>
  <h1>{post.title}</h1>
  <div>{post.content}</div>
</article>

Database Schema Automatically Created: Tables are created and seeded on first deployment:

CREATE TABLE IF NOT EXISTS posts (
  id SERIAL PRIMARY KEY,
  title TEXT NOT NULL,
  description TEXT,
  content TEXT NOT NULL,
  author TEXT NOT NULL,
  pub_date TIMESTAMP DEFAULT NOW(),
  hero_image TEXT,
  slug TEXT UNIQUE NOT NULL,
  created_at TIMESTAMP DEFAULT NOW()
);

PostgreSQL Connection: Automatically uses Railway's database URL:

import postgres from 'postgres';

const connectionString = process.env.DATABASE_PUBLIC_URL;
export const sql = postgres(connectionString, {
  ssl: 'require',
  max: 10,
});

Adding New Content: Insert posts directly via PostgreSQL:

INSERT INTO posts (title, description, content, author, slug, hero_image)
VALUES (
  'My New Post',
  'A brief description',
  'Full post content here...',
  'Author Name',
  'my-new-post',
  'https://example.com/image.jpg'
);

API Endpoints: Create API routes for data access:

// src/pages/api/posts.json.ts
export const GET: APIRoute = async () =&gt; {
  const posts = await sql`SELECT * FROM posts`;
  return new Response(JSON.stringify(posts), {
    headers: { 'Content-Type': 'application/json' }
  });
};

Tailwind Styling: Pre-configured with responsive design:

<div class="container mx-auto px-4 py-8 max-w-6xl">
  <h1 class="text-5xl font-bold mb-8">Blog</h1>
  <div class="grid gap-8 md:grid-cols-2 lg:grid-cols-3">
    
  </div>
</div>

Performance Optimizations:

  • Static HTML generation with server-side data fetching
  • Zero JavaScript shipped by default (< 1KB page weight)
  • PostgreSQL connection pooling with 10 max connections
  • Docker multi-stage builds reduce image size to ~200MB
  • Automatic SSL termination by Railway

Database Initialization: Automatic setup on deployment:

export async function initDatabase() {
  await sql`CREATE TABLE IF NOT EXISTS posts (...)`;
  const count = await sql`SELECT COUNT(*) FROM posts`;
  if (count[0].count === 0) {
    await sql`INSERT INTO posts VALUES (...)`;
  }
}

// Auto-run on server start
initDatabase().catch(console.error);

Extending the Schema: Add new tables easily:

await sql`
  CREATE TABLE IF NOT EXISTS comments (
    id SERIAL PRIMARY KEY,
    post_id INTEGER REFERENCES posts(id),
    author TEXT NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
  );
`;

Lighthouse Scores:

  • Performance: 100
  • Accessibility: 100
  • Best Practices: 100
  • SEO: 100

Response Times:

  • Home page: < 50ms
  • Blog post: < 100ms
  • Database query: < 10ms
  • Build time: ~30 seconds

Why Deploy Astro + PostgreSQL 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 Astro + PostgreSQL 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.

Additional Benefits:

  • Zero Configuration - Database, migrations, and environment variables set up automatically
  • Instant Deployment - From git push to live site in under 2 minutes
  • Automatic Scaling - Astro's efficiency handles high traffic on minimal resources
  • Built-in Monitoring - Health checks and deployment logs included
  • Database Management - PostgreSQL provisioned, backed up, and managed by Railway
  • Cost-Effective - Astro's small footprint means lower hosting costs
  • Developer Experience - Live reloads, instant previews, and zero build configuration
  • Production Ready - SSL, CDN, and database backups included out of the box

Template Content

More templates in this category

View Template
caring-vibrancy
Deploy and Host caring-vibrancy with Railway

View Template
Mellow Vue
A full-stack Vue app with The Boring JavaScript Stack.

View Template
NextJS 15.5 (Server Actions) with Shadcn
Better-Auth Ready with Auth flows and Server actions (Prod ready)