question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't connect to a SQL Server database with a named instance

See original GitHub issue

Bug description

At work, I presented Prisma to my manager, and he liked it, so we want to use it in our next project. Sometimes we use MySQL, but for this project we are using SQL Server (probably Express).

The problem is trying to connect with a named instance (which is probably going to be a requirement for the project). At work, we have Windows 10 machines, and this example is from home with a Windows 11 machine. The same error occurs.

So I have 2 servers running:

  • MSSQL Server
  • SQL Express

Some picture of my setup: image image image image

TCP/IP is enabled for both.

Here I tried connecting using the ‘sqlcmd’ and it works for both connections. image

Here are connection strings I have tried and they didn’t work:

// also for every string I also tried using \\ instead of just \  inbetween the HOST and INSTANCE

// this would be the ideal form to have working
DATABASE_URL="sqlserver://DESKTOP-F3USMA8\SQLEXPRESS;database=prisma;integratedSecurity=true;trustServerCertificate=true;"

DATABASE_URL="sqlserver://DESKTOP-F3USMA8\MSSQLSERVER;database=prisma;integratedSecurity=true;trustServerCertificate=true;"

DATABASE_URL="sqlserver://DESKTOP-F3USMA8\SQLEXPRESS:2584;database=prisma;integratedSecurity=true;trustServerCertificate=true;"

DATABASE_URL="sqlserver://localhost\SQLEXPRESS:2584;database=prisma;integratedSecurity=true;trustServerCertificate=true;"

Here are connection strings that worked:

// the ports and instances are in the pictures above

// connects to the SQL Express instance
DATABASE_URL="sqlserver://localhost:2584;database=prisma;integratedSecurity=true;trustServerCertificate=true;"

// connects to the SQL Server instance
DATABASE_URL="sqlserver://localhost:2584;database=prisma;integratedSecurity=true;trustServerCertificate=true;"

// connects to the SQL Server instance
DATABASE_URL="sqlserver://DESKTOP-F3USMA8;database=prisma;integratedSecurity=true;trustServerCertificate=true;"

From this, I can see that the named instance connection string are not working.

I could probably test it on my laptop too, with a fresh installation of windows.

How to reproduce

Using

 npx prisma db push

creates this error:

Error creating a database connection.

Expected behavior

Prisma establishes connection to the SQL Server DB with a named instance.

Prisma information

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlserver"
  url      = env("DATABASE_URL")
}

model Post {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  title     String   @db.VarChar(255)
}

Environment & setup

  • OS: Windows 11/10
  • Database: SQL Server (Developer, Express)
  • Node.js version: 18.7.0

Prisma Version

prisma                  : 4.2.1
@prisma/client          : 4.2.1
Current platform        : windows
Query Engine (Node-API) : libquery-engine 2920a97877e12e055c1333079b8d19cee7f33826 (at node_modules\@prisma\engines\query_engine-windows.dll.node)     
Migration Engine        : migration-engine-cli 2920a97877e12e055c1333079b8d19cee7f33826 (at node_modules\@prisma\engines\migration-engine-windows.exe) 
Introspection Engine    : introspection-core 2920a97877e12e055c1333079b8d19cee7f33826 (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt 2920a97877e12e055c1333079b8d19cee7f33826 (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : 2920a97877e12e055c1333079b8d19cee7f33826
Studio                  : 0.469.0

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pimeyscommented, Sep 22, 2022

Actually, now when I’m looking at this once more, this is a bug of a feature that should work.

0reactions
PointSingularitycommented, Sep 22, 2022

Hey, can you confirm do you want named pipes support or just use the SQL Browser together with TCP. If it’s the latter, did you try to add the name with two backslashes?

E.g.

sqlserver://localhost\instance1;integratedSecurity=…

Hey, to clarify I want to use a named instance of a server, to connect through tcp.

image

I tried your suggestion with these options, and none of them work (find the port with the method from above):

DATABASE_URL="sqlserver://localhost\\SQLEXPRESS;database=taa;integratedSecurity=true;trustServerCertificate=true;"
DATABASE_URL="sqlserver://localhost\\SQLEXPRESS:2584;database=taa;integratedSecurity=true;trustServerCertificate=true;"
DATABASE_URL="sqlserver://DESKTOP-F3USMA8\\SQLEXPRESS;database=taa;integratedSecurity=true;trustServerCertificate=true;"
DATABASE_URL="sqlserver://DESKTOP-F3USMA8\\SQLEXPRESS:2584;database=taa;integratedSecurity=true;trustServerCertificate=true;"

Errors out with: Error parsing connection string: Conversion error: Invalid instance name in database URL. Please refer to the documentation in https://www.prisma.io/docs/reference/database-reference/connection-urls for constructing a correct connection string. In some cases, certain characters must be escaped. Please check the string for any illegal characters."

DATABASE_URL="sqlserver://localhost\SQLEXPRESS;database=taa;integratedSecurity=true;trustServerCertificate=true;"
DATABASE_URL="sqlserver://localhost\SQLEXPRESS:2584;database=taa;integratedSecurity=true;trustServerCertificate=true;"
DATABASE_URL="sqlserver://DESKTOP-F3USMA8\SQLEXPRESS;database=taa;integratedSecurity=true;trustServerCertificate=true;"
DATABASE_URL="sqlserver://DESKTOP-F3USMA8\SQLEXPRESS:2584;database=taa;integratedSecurity=true;trustServerCertificate=true;"

Errors out with: invocation:\n\n\nError creating a database connection. (Error creating a database connection.)"

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't connect using named instance from 1 client
1) Make sure your server name is correct, e.g., no typo on the name. ... 3) Make sure the server machine is reachable,...
Read more >
Cannot connect remotely to a SQL Server named instance
You need to start the SQL Server Browser service on the VM that hosts the SQL Server instances. You can find this by...
Read more >
Resolving could not open a connection to SQL Server errors
Check to see if allow remote connections for this server is enabled. In SSMS, right click on the instance name and select Properties....
Read more >
Unable to connect to named instance. - SQLServerCentral
There is a server running SQL Server 2012 32 bit. This is the default instance. We can connect to this remotely. We installed...
Read more >
sql server - Cannot connect to remote instance
Open port on the server to TCP 1433 - Inbound Rules · SQL Server Configuration Manager - SQL Server (mssqlserver), SQL Server Browser,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found