DataMosaic

Blog · How-to

How to connect n8n to SQL Server: credential, query, insert

Every field of n8n's Microsoft SQL credential and what to put in it, then one query with parameters and one insert, from n8n's own documentation.

#What you need before you start

  • n8n where it can reach the server. A self-hosted n8n inside your network reaches SQL Server directly. n8n Cloud reaches it only if your firewall lets in n8n's outbound IP addresses. Many instances share each address, and n8n says an instance's address "can change without notice", so it recommends allowlisting every address on the list and always requiring authentication.
  • A SQL Server login. n8n's credential page has one prerequisite: "Create a user account on a Microsoft SQL server database." The server must accept SQL Server authentication, not Windows authentication alone.
  • The port. SQL Server defaults to 1433. If that fails, n8n's documentation says to look in SQL Server's error logs for "Server is listening on".

If n8n runs in Docker on the same machine as SQL Server, localhost in the credential means the n8n container, not the machine. Use the server's name or address on your network.

#Step 1: a login that can do only the job

Give the workflow its own login, with the rights it needs and no others. Run this in SSMS. POS_HQ and its tables are invented: put in your own names.

USE master;
GO
CREATE LOGIN n8n_worker WITH PASSWORD = 'Replace-with-a-long-passphrase-2026';
GO

USE POS_HQ;
GO
CREATE USER n8n_worker FOR LOGIN n8n_worker;
GO

-- Read the receipts table, and nothing else in the database.
GRANT SELECT ON dbo.pos_receipts TO n8n_worker;
GO

-- A table the workflow may write to, for the insert in step 4.
CREATE TABLE dbo.store_day_totals (
    store         varchar(10)   NOT NULL,
    business_date date          NOT NULL,
    receipts      int           NOT NULL,
    net_sales     decimal(12,2) NOT NULL,
    loaded_at     datetime2     NOT NULL DEFAULT SYSUTCDATETIME(),
    CONSTRAINT PK_store_day_totals PRIMARY KEY (store, business_date)
);
GO
GRANT INSERT ON dbo.store_day_totals TO n8n_worker;
GO

The primary key on store and business date means that a second run for the same day fails on a duplicate rather than loading the day twice. Step 4 comes back to that.

#Step 2: the Microsoft SQL credential

In n8n, open Credentials, create a new one and search for Microsoft SQL. These are its fields, with the defaults the credential ships with in November 2026 and what n8n's documentation says about each.

FieldDefaultWhat to put in it
ServerlocalhostThe host name, or HOSTNAME\INSTANCENAME for a named instance
DatabasemasterThe database the workflow works in, here POS_HQ
UsersaThe login from step 1, n8n_worker. Never leave it on sa
PasswordemptyThe login's password
Port1433The port the instance listens on
Domainemptyn8n's documentation: only needed "if users in multiple domains access your database"
TLSOnLeave it on
Ignore SSL Issues (Insecure)OffSee step 3
Connect Timeout15000Milliseconds: 15000 is 15 seconds
Request Timeout15000Milliseconds. n8n calls it "basically a query timeout parameter"
TDS Version7_4Leave it at 7_4, labelled SQL Server 2012 ~ 2019, unless the server is older

Two of these catch people out.

Database is the database. The node opens the database named in Database, and a named instance goes in Server as HOSTNAME\INSTANCENAME. Leave Database on its default and every query runs in master, where dbo.pos_receipts does not exist.

The timeouts are in milliseconds. SQL Server tools count timeouts in seconds, so a value copied from them needs multiplying by 1,000. The default Request Timeout of 15 seconds is short for a query that totals a full day of receipts: raise it to 120000 for a nightly job, or make the query faster.

Save the credential. n8n tests it as it saves and shows Connection successful! or the error SQL Server returned.

#Windows authentication

n8n's credential page lists "SQL database connection" as its only authentication method, and does not describe signing in with a Windows account. Plan for a SQL Server login. If the server allows Windows authentication only, a database administrator has to allow SQL Server authentication as well before n8n can sign in.

#Step 3: TLS and the server's certificate

TLS is on by default, so the connection is encrypted and n8n checks the certificate the server presents. Many SQL Server instances inside a company present a certificate they made themselves, and that check then fails. You have two ways out:

  1. Install the certificate of the authority that issued the server's certificate where n8n runs, so the check passes. This keeps the check.
  2. Turn on Ignore SSL Issues (Insecure). In n8n's words, the credential "will connect even if SSL certificate validation fails". The traffic is still encrypted, but nothing proves that the server is the one you meant, so do this only on a network you trust.

#Step 4: one query, then one insert

The Microsoft SQL node has four operations: Execute Query, Insert, Update and Delete.

#A query with parameters

  1. Add a Schedule Trigger, then a Microsoft SQL node, and pick the credential from step 2.
  2. Set Operation to Execute Query.
  3. Paste the query into Query:
SELECT store,
       business_date,
       COUNT(*)        AS receipts,
       SUM(net_amount) AS net_sales
FROM dbo.pos_receipts
WHERE business_date = $1
GROUP BY store, business_date
ORDER BY store;
  1. Under Options, choose Add option › Query Parameters, and enter yesterday's date as an expression:
{{ $now.minus({ days: 1 }).toFormat('yyyy-MM-dd') }}
  1. Select Execute step. Each row comes back as one item, with the columns as its fields.

The node's own hint explains why step 4 matters: "Consider using query parameters to prevent SQL injection attacks." Query Parameters is a comma-separated list, and $1, $2, $3 in the query refer to its values in order. The node sends them to SQL Server as parameters rather than pasting them into the text of the query. A query that names $3 when only two values are set fails with a message saying so.

$now follows the instance's time zone. Set the workflow's Timezone in its settings, or the GENERIC_TIMEZONE variable on a self-hosted instance, so that "yesterday" is yesterday where your stores are. The n8n vs Power Automate post has the same job built in both tools.

If a query returns more than one result set, the node puts all their rows into one list of items.

#An insert

  1. Add a second Microsoft SQL node after the query, with the same credential.
  2. Set Operation to Insert.
  3. Table: dbo.store_day_totals.
  4. Columns: store,business_date,receipts,net_sales.

Each item's fields with those names become a row, so the names in Columns must match the fields the previous node returned. loaded_at is left out and takes its default. The node writes rows in batches under SQL Server's limit of 2,100 parameters a statement, so a few thousand rows need no extra work.

Update works the same way with an Update Key, id by default, naming the column that finds the row to change. Delete takes a Delete Key.

#When a run fails

Because of the primary key in step 1, running the workflow twice for one day fails on the second run. That is what you want, and n8n should tell someone. Set an Error Workflow in the workflow's settings, starting with the Error Trigger, that emails IT. Each node also has Retry On Fail in its settings: worth turning on for the query, which can meet a busy server, and not for the insert, which should fail loudly.

#A week of the workflow, worked through

An invented chain of 120 stores runs the workflow at 05:30. It reads yesterday's totals from POS_HQ and inserts them into store_day_totals, where the ERP's nightly load picks them up. Here is a week, and what was true in the ERP by 07:00.

MorningExecutionRows insertedWhat was true in the ERP
MondaySucceeded120ST-014 at 11,920.00 against 12,400.00 at the tills, 480 apart
TuesdayFailed: request timeout0Nothing for any store. The Error Workflow emailed IT, who raised Request Timeout to 120000
WednesdaySucceeded119ST-022's 6,410.75 reached POS_HQ after 05:30, so it was missing in both
ThursdaySucceeded120All 120 agreed
FridaySucceeded120ST-031 at 8,119.96 against 8,120.00 at the tills, 0.04 of rounding

Invented figures. The workflow did what it was built to do: it connected, read and inserted, and the one morning it could not, IT heard about it. Three of the four green mornings still hid something. On Monday the ERP took 120 rows and booked one of them 480 short. On Wednesday 119 rows was a correct copy of an incomplete day. On Friday 0.04 was rounding, and nothing needed doing. An execution that succeeded says the rows moved. It does not say the two systems agree.

#Where n8n stops

You now have n8n reading from and writing to your own SQL Server, without Mosaic. To know every morning whether the ERP agrees with the tills, you would add a node that reads the ERP too, join the two lists on store and date, handle a store missing on one side, write down a tolerance so that ST-031 sends no email, and keep each night's answer to set against the next. The queries for that comparison are in compare two tables in SQL.

DataMosaic does that part. It can run on a server inside your network and read SQL Server with a read-only login, with the same choices about encryption and a server's own certificate. A check reads two systems, lines their rows up on a key such as store and business date, and shows each store as matched, differing or missing. Rules decide whether a run passed, so 0.04 of rounding does not fail it. On a row that differs, an investigation works through the reasons one system can miss or refuse the other's data, until ST-014's 480 has one. A check only reads. Mosaic changes a system only through a workflow you build, started by a check, a schedule, a call or by hand. It is not a general automation tool: keep n8n for the jobs that move data, and see the Power Automate alternatives post if you are still choosing one.

#Questions people ask

#Can n8n connect to SQL Server with Windows authentication?

n8n's documentation for the Microsoft SQL credential lists "SQL database connection" as its only authentication method and does not describe Windows authentication. Use a SQL Server login, which means the server must accept SQL Server authentication.

#What port does n8n use for SQL Server?

The credential's Port defaults to 1433, SQL Server's default. If your instance listens on another port, SQL Server's error log names it after "Server is listening on".

#Why does n8n fail on my SQL Server's certificate?

TLS is on by default, and n8n checks the server's certificate. A certificate the server made itself fails that check: install the issuing authority's certificate where n8n runs, or turn on Ignore SSL Issues (Insecure) on a network you trust.

#How do I pass a value into a query in the Microsoft SQL node?

Write $1, $2 in the query, and put the values, separated by commas, in Options › Query Parameters. The node sends them to SQL Server as parameters, which protects the query from SQL injection.

#Why does my n8n SQL query time out after 15 seconds?

The credential's Request Timeout defaults to 15000 milliseconds. Raise it in the credential, or make the query faster, often with an index on the column it filters by.

Stop comparing exports by hand.

DataMosaic checks the systems themselves overnight, tells you why they differ, and runs the fix you've set up, before anyone else notices.