# How to Use a Microsoft SQL Server Database with Yurbi on Docker

Source: https://help.yurbi.com/admin-guides/how-to-use-a-microsoft-sql-server-database-with-yurbi-on-docker

The Yurbi Docker package installs with its own PostgreSQL database. This guide explains how to switch a new Docker installation to a Microsoft SQL Server database that you host yourself.

This is a manual procedure for the less common case where Yurbi's own database must live on SQL Server. You do **not** need it to report on SQL Server data: Yurbi can connect to SQL Server data sources whichever database it uses for itself.

## Before you start

- **Do this on a new installation**, before you activate your license, add users or build reports. Nothing is copied from the PostgreSQL database to SQL Server. Data cannot be migrated between the two later, so choose the database you will keep.
- **SQL Server authentication only.** The SQL Server must accept SQL logins (user name and password). Windows authentication is supported only when Yurbi is installed on Windows Server.
- **SQL Server must support TLS 1.2.** Very old, unpatched SQL Server versions that only offer older encryption cannot be used.
- **The password** of the SQL login must be 32 characters or fewer.
- **Network.** The Docker server must be able to reach the SQL Server on its port (1433 by default).

You need:

- Yurbi installed with Docker and running. See the Docker install guide.
- A database administrator who can create a database and a login on the SQL Server.

This guide assumes the installation folder is `/opt/yurbi`.

## Step 1: Create the database on SQL Server

Your database administrator does this on the SQL Server, for example with SQL Server Management Studio.

1. Create an empty database named `yurbi`.
2. Create a SQL login for Yurbi and make it a member of `db_owner` on the `yurbi` database.
3. Download the database script:
    
    ```plaintext
    https://f002.backblazeb2.com/file/5000fish/yurbi/linux/script.sql
    
    ```
4. Run the script against the `yurbi` database. It creates the Yurbi tables and starter data.

## Step 2: Test the connection from the Docker server

Confirm that the Yurbi container can reach the SQL Server and log in. Replace the server name and login with your own. You are asked for the password.

```bash
cd /opt/yurbi
sudo docker compose exec yurbi /opt/mssql-tools18/bin/sqlcmd -S sqlserver.example.com,1433 -U yurbi_login -d yurbi -C -Q "select count(*) as tables from sys.tables"

```

It should print a table count greater than zero. If it reports a connection or login error, resolve that before going further. Common causes are a firewall, a wrong port, or SQL logins not being enabled on the server.

Use the form `server,port` for the server. If your SQL Server uses a named instance, ask your database administrator for its port number and use that, rather than `server\instance`.

## Step 3: Point Yurbi at SQL Server

The database connection is set in the `windowservices.config` file, which is stored inside the installation. Copy it out, edit it, and copy it back.

1. Copy the file out and keep a spare copy:
    
    ```bash
    cd /opt/yurbi
    sudo docker cp yurbi-app:/var/yurbi/config/windowservices.config ./windowservices.config
    sudo cp windowservices.config windowservices.config.postgres-backup
    
    ```
2. Open the file in an editor:
    
    ```bash
    sudo nano windowservices.config
    
    ```
3. Find the line that starts with `<Database`. It looks like this:
    
    ```xml
    <Database Server="postgres:5432" Username="postgres" Password="(a long encrypted value)" Database="yurbi" DatabaseType="Npgsql"/>
    
    ```
4. Replace that one line with the following, using your own server, login and password:
    
    ```xml
    <Database Server="sqlserver.example.com,1433;TrustServerCertificate=True;Encrypt=false" Username="yurbi_login" Password="YourPassword" Database="yurbi" DatabaseType="System.Data.SqlClient"/>
    
    ```
    
    
    - Keep `;TrustServerCertificate=True;Encrypt=false` after the server name exactly as shown.
    - Type the password as plain text. Yurbi encrypts it in the file the first time it starts.
    - If the password contains `&`, `<`, `>` or `"`, write them as `&amp;`, `&lt;`, `&gt;` and `&quot;`.
    - Do not change any other line.
5. Copy the file back and restart Yurbi:
    
    ```bash
    sudo docker cp ./windowservices.config yurbi-app:/var/yurbi/config/windowservices.config
    sudo docker compose restart yurbi
    
    ```
6. Remove the edited copy, because it contains the password in plain text:
    
    ```bash
    sudo rm ./windowservices.config
    
    ```

## Step 4: Check the result

1. Wait about a minute, then check that the application is healthy:
    
    ```bash
    sudo docker compose ps
    
    ```
2. Confirm the password in the file is now encrypted, and that the file points at SQL Server:
    
    ```bash
    sudo docker compose exec yurbi grep "<Database" /var/yurbi/config/windowservices.config
    
    ```
    
    You should see `DatabaseType="System.Data.SqlClient"` and a long encrypted value in place of the password you typed.
3. Open Yurbi in your browser and log in with the default administrator account.
4. Activate your license, then change the administrator password.

If the login fails, look for database errors:

```bash
sudo docker compose exec yurbi sh -c 'grep -h ":##:ERROR:##:" /var/yurbi/logs/* | tail -20'

```

## What changes once you use SQL Server

- **Backups are yours to arrange.** The Yurbi upgrade script backs up only the PostgreSQL database that came with the package, which is no longer in use. Make sure your SQL Server backups include the `yurbi` database, and take one before every Yurbi upgrade.
- **Upgrades work the same way.** Run `upgrade.sh` as described in the Docker install guide. Your connection setting is kept.
- **The PostgreSQL container keeps running.** It is no longer used, but leave it in place. The package expects it to be there.
- **Changing the SQL password later.** Repeat step 3 with the new password.

## Going back to the built-in database

To return to the PostgreSQL database that came with the package:

```bash
cd /opt/yurbi
sudo docker cp ./windowservices.config.postgres-backup yurbi-app:/var/yurbi/config/windowservices.config
sudo docker compose restart yurbi

```

Anything created while Yurbi was using SQL Server stays in SQL Server and does not appear in the PostgreSQL database.
