Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post

Connecting QGIS to MSSQL: A Step-by-Step Tutorial

Step 1: Launch QGIS

Start by opening QGIS on your computer. This is the software that will allow you to connect to your MSSQL database and visualise the data.

Opening QGIS software

Step 2: Add a Layer

Once QGIS is running, navigate to the menu and click on Layers. You will then select Add Layer and choose Add MSSQL Spatial Layer, which connects to Microsoft SQL Server.

Navigating to add a layer in QGIS

Step 3: Create a New Connection

In the window that opens, click on New to create a new connection. You will need to provide a name for this connection.

Creating a new connection in QGIS

Step 4: Configure the Connection Settings

Input the details for your database, including the table you are connecting to. Ensure that the host is set — this can be an IP address or the name of the server.

Note that there may be a bug in the connection testing feature. If you click on Test Connection and see an error message, do not worry; this does not necessarily mean the connection has failed.

Configuring connection settings

Step 5: List Databases

Instead of testing the connection, click on List Databases. This should display all available databases on the server.

Listing databases in QGIS

Step 6: Connect to the Database

After selecting the database, click OK. You will now see the various objects you can connect to within the database. At this point, there may not be any visible objects if you have not imported data yet.

Connecting to the database

Step 7: Add the Data as a Layer

Locate the table that you are interested in. Select it to add it as a layer in QGIS.

Adding data as a layer in QGIS

Step 8: View the Data

After adding the layer, you will see the data displayed on the map. These points represent geospatial data retrieved from the database.

Viewing points on the map

Step 9: Check the Attribute Table

To understand what each point represents, you can open the attribute table. Here, you will find the attribute values — such as latitude and longitude — associated with each feature.

Opening the attribute table

Step 10: Create a Basemap Layer

To provide geographic context, add a basemap layer such as OpenStreetMap. This allows you to overlay the data points on a map and visualise their locations more effectively.

Creating a basemap layer

Conclusion

Your QGIS is now successfully connected to the MSSQL database, and you can visualise the data as needed. This setup is essential for working with geospatial data and validating information imported from your database.

Frequently asked questions (FAQs) about connecting QGIS to Microsoft SQL Server

What are the prerequisites for connecting QGIS to MSSQL?

To connect QGIS to Microsoft SQL Server, ensure the following prerequisites are met:

  • QGIS installed: Ensure that you have QGIS installed on your computer. The latest version is recommended for compatibility and access to the most recent features.
  • Microsoft SQL Server access: You need access to an instance of MSSQL, either locally or on a remote server.
  • Database credentials: You will need a username, password, database name, and server address for authentication.
  • ODBC driver: Install the appropriate ODBC driver for SQL Server. This driver enables communication between QGIS and MSSQL. The driver can be downloaded from the Microsoft ODBC Driver download page.

How do I install the ODBC driver for SQL Server?

To install the ODBC driver for SQL Server:

  1. Download the driver: Visit the Microsoft ODBC Driver for SQL Server download page on Microsoft Learn and download the appropriate driver for your operating system (Windows, macOS, or Linux). The current recommended version is ODBC Driver 18 for SQL Server.
  2. Run the installer: Follow the instructions in the installer to install the driver on your system.
  3. Configure the driver: After installation, configure a Data Source Name (DSN) using the ODBC Data Source Administrator on Windows, or the equivalent tool on your operating system.

How do I add a new MSSQL connection in QGIS?

To add a new MSSQL connection in QGIS:

  1. Open QGIS: Launch QGIS.
  2. Go to Data Source Manager: Click on the Layer menu, then select Add Layer > Add MSSQL Spatial Layer.
  3. Create a new connection: Click New to create a new connection.
  4. Enter connection details:
    • Name: Enter a name for your connection.
    • Host: Enter the server address where MSSQL is hosted.
    • Port: The default port for SQL Server is 1433, unless it has been configured otherwise.
    • Database: Enter the name of the MSSQL database you wish to connect to.
    • Username and password: Enter your SQL Server authentication credentials.
  5. Test connection: Click Test Connection to verify the settings.
  6. Save and connect: If the test is successful, click OK to save the connection.

How do I configure QGIS to use Windows Authentication for connecting to MSSQL?

To use Windows Authentication when connecting QGIS to MSSQL:

  1. Enable Windows Authentication: Ensure that your SQL Server instance is configured to allow Windows Authentication.
  2. In QGIS connection settings: In the Add MSSQL Spatial Layer dialog, select Windows Authentication as the authentication type.
  3. Leave username and password blank: When using Windows Authentication, QGIS automatically uses the credentials of the currently logged-in Windows user.
  4. Test connection: Click Test Connection to verify that QGIS can connect using your Windows credentials.

What are the common errors when connecting QGIS to MSSQL, and how do I troubleshoot them?

Common errors and their troubleshooting steps:

  • “Could not connect to the database” error: Check that the server name, database name, port, username, and password are correct. Ensure that the SQL Server instance is running and accessible from the network.
  • “No ODBC driver found” error: Confirm that the correct ODBC driver for SQL Server is installed and that it is compatible with your operating system and SQL Server version.
  • “Login failed” error: Verify that the username and password are correct and that the account has the required permissions on the SQL Server instance.
  • Firewall issues: Ensure that the firewall on both the SQL Server and client machine allows traffic on the SQL Server port (default: 1433).

How do I visualise MSSQL spatial data in QGIS?

Once you have connected to MSSQL in QGIS, you can visualise spatial data as follows:

  1. Open the MSSQL Spatial Layer dialog: Go to Layer > Add Layer > Add MSSQL Spatial Layer.
  2. Select your connection: Choose the connection you created earlier.
  3. List available tables: Click List Tables to display the spatial tables in the database.
  4. Select the table: Choose the desired spatial table and click Add to load it into QGIS.
  5. Visualise data: The selected spatial data will appear as a new layer in QGIS, where you can style and analyse it using the full range of QGIS tools.

Can I edit MSSQL spatial data directly in QGIS?

Yes, you can edit MSSQL spatial data directly in QGIS, provided the following conditions are met:

  • Edit permissions: Ensure you have the necessary write permissions on the SQL Server database.
  • Toggle editing: Select the MSSQL layer in the Layers panel and click the Toggle Editing button.
  • Make edits: Use QGIS editing tools to modify geometries, attributes, or add new features.
  • Save edits: After making changes, click Save Layer Edits to commit the changes to the database.

Ensure that you have appropriate backups before editing, as changes are written directly to the SQL Server database.

How do I handle large datasets when connecting QGIS to MSSQL?

When dealing with large datasets in QGIS connected to MSSQL:

  • Use SQL queries: Use the SQL filter in the Add MSSQL Spatial Layer dialog to load only the subset of data you need.
  • Limit rendering by scale: Set a layer scale range to prevent QGIS from rendering the full dataset when zoomed out.
  • Optimise the database: Ensure the SQL Server database has appropriate spatial indexes and query optimisation in place.
  • Use data sampling: Load a representative subset of the data for initial analysis before working with the full dataset.

Can I use QGIS to run SQL queries on an MSSQL database?

Yes, QGIS provides the DB Manager to run SQL queries directly against a connected database:

  1. Open DB Manager: Go to Database > DB Manager.
  2. Select the MSSQL connection: Expand the MSSQL provider and choose your database connection.
  3. Open the SQL window: Click the SQL Window button to open a query editor.
  4. Run queries: Enter your SQL queries and click Execute to run them.

You can use this feature to perform data analysis, create views, or modify data directly from QGIS.

How do I secure my MSSQL connection in QGIS?

To secure your MSSQL connection in QGIS:

  • Use encrypted connections: Enable SSL/TLS encryption on the SQL Server instance and configure QGIS to use encrypted connections.
  • Secure credentials: Store connection credentials using the QGIS authentication database rather than saving them in plain text within the connection settings.
  • Apply the principle of least privilege: Grant the connecting database user only the permissions required for their tasks.
  • Keep software updated: Keep both QGIS and SQL Server updated to ensure you have the latest security patches.
About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.