/n8n-tutorials

How to use n8n with self-hosted databases?

Learn how to connect and automate your self-hosted databases with n8n using secure integrations and efficient workflows in this step-by-step guide.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to use n8n with self-hosted databases?

You use n8n with self‑hosted databases by giving n8n network access to the database host, creating proper n8n Database Credentials (Postgres, MySQL, Mongo, etc.), and then using the built‑in database nodes (like Postgres, MySQL, MongoDB) to run queries. In real production, the main things that matter are: networking, credentials, SSL, connection permissions, and keeping queries small and efficient so n8n doesn’t become your database engine.

 

How it actually works in production

 

n8n doesn’t magically “scan” your database — it simply acts like any normal app that connects to a database. It opens a connection, runs your SQL (or Mongo ops), returns the result as JSON, and passes that JSON to the next node.

To connect n8n to a self‑hosted DB, you only need:

  • Network access between your n8n server/container and the database server/container
  • Correct credentials (username, password, host, port, database name)
  • Correct node type (Postgres Node, MySQL Node, MongoDB Node, etc.)
  • Secure connection settings (SSL if your DB requires it)

Once that’s set, your workflow can run INSERT/UPDATE/SELECT queries directly from n8n, and the results become the JSON for the next node.

 

Step‑by‑step: Connecting a self‑hosted DB

 

This is the typical pattern that works in Docker, bare‑metal servers, or cloud VMs.

  • Make sure the database is reachable from n8n. If both run in Docker, use the Docker network and containers can talk by name. If the DB is outside Docker, expose the port safely (usually 5432 for Postgres, 3306 for MySQL).
  • Create credentials in n8n. In n8n: Settings → Credentials → choose Postgres/MySQL/Mongo → fill host, port, user, password. n8n stores these securely.
  • Drop a database node into a workflow. Use the correct node type (Postgres Node, MySQL Node, MongoDB Node). Choose an operation: Execute Query for SQL DBs, or Insert/Find/Update for Mongo.
  • Use expressions to write dynamic queries. Example: read JSON from previous node and insert it.

 

Example: Postgres insert using JSON from previous node

 

This uses the Postgres node with Operation = “Execute Query”.

INSERT INTO users (email, name)
VALUES ({{$json.email}}, {{$json.name}});

n8n replaces {{$json.email}} with actual values from the previous node before running the SQL.

 

Example: Reading from Postgres

 

SELECT id, email, created_at
FROM users
WHERE created_at > NOW() - INTERVAL '1 day';

This returns an array of records as JSON items, perfect for loops, merges, or sending to APIs.

 

Production‑level considerations

 

  • n8n is not a database engine. Do heavy joins, mass updates, or large exports directly in your DB or API layer. Keep the data chunks small when possible.
  • Connection timeouts. n8n nodes have execution time limits depending on your environment. Long‑running DB queries cause workflow failures.
  • Pagination matters. If your query returns thousands of rows, loop through them in chunks.
  • Use error workflows. If your DB occasionally drops connections, catch failures and retry.
  • Secure your network. Use private networking or VPN to avoid exposing your DB directly to the internet.

 

If you're self‑hosting n8n in Docker

 

A very common setup is running n8n and the DB in the same Docker network.

// docker-compose.yml (simplified)
services:
  n8n:
    image: n8nio/n8n
    depends_on:
      - postgres
    networks:
      - internal

  postgres:
    image: postgres:15
    environment:
      POSTGRES_USER: n8n
      POSTGRES_PASSWORD: n8npass
    networks:
      - internal

networks:
  internal:
    driver: bridge

Inside n8n, the host would simply be:

postgres

Because Docker resolves it by container name.

 

The core idea

 

As long as n8n can reach your self‑hosted database and you provide valid credentials, you can query it like any normal application. n8n nodes simply turn query results into JSON and pass them along the workflow, making database operations easy to integrate with APIs, logic nodes, and automations.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022