> ## Documentation Index
> Fetch the complete documentation index at: https://syteca.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Moving the PostgreSQL Database

> Relocate the PostgreSQL database to another drive using tablespaces, or migrate the entire PGDATA directory to a new computer.

<Warning>
  **NOT AVAILABLE IN SAAS.**
</Warning>

## Move the database on the same computer

This moves the database to a different drive using a PostgreSQL tablespace, without changing computers.

<Steps>
  <Step title="Stop the Application Server">
    Stop the **EkranServer** service.
  </Step>

  <Step title="Identify the PostgreSQL service account">
    Open `services.msc`, find the **postgresql-xxx** service, and note the account it runs as (for example, **Network Service**).
  </Step>

  <Step title="Create and permission the new folder">
    Create a folder on the target drive, then on its **Properties > Security** tab, add the PostgreSQL service account and grant it full permissions.
  </Step>

  <Step title="Create a tablespace">
    In pgAdmin, right-click **Tablespaces**, select **Create > Tablespace…**, name it, and point it at the folder created above. Click **Save**.
  </Step>

  <Step title="Move each Syteca database to the new tablespace">
    For `ekranactivitydb`, `EkranManagementDatabase`, and `EkranUbaDatabase`: right-click the database, open **Properties > Definition**, set **Tablespace** to the one created above, and click **Save**.

    <Note>
      The transfer completes once the confirmation pop-up closes.
    </Note>
  </Step>

  <Step title="Close any open connections if the move fails">
    If open connections block the transfer, run the following (replacing `database_name`), then retry step 5:

    ```sql theme={"system"}
    SELECT pg_terminate_backend (pg_stat_activity.pid)
    FROM pg_stat_activity
    WHERE pg_stat_activity.datname = 'database_name';
    ```
  </Step>
</Steps>

## Move the database to another computer

By default, PostgreSQL for Windows stores its data in `C:\Program Files\PostgreSQL\<version>\data` (the PGDATA directory).

<Steps>
  <Step title="Stop the Application Server">
    Open Windows Services and stop the **EkranServer** service.
  </Step>

  <Step title="Stop PostgreSQL on both computers">
    Stop the **PostgreSQL** service on both the old and new PostgreSQL servers.

    <Warning>
      Check Task Manager for any still-running `postgresql.exe` processes. Don't terminate them directly — instead, close whatever applications are still connected to the database.
    </Warning>
  </Step>

  <Step title="Move the PGDATA directory">
    Move the entire PGDATA directory to the new machine, which must have the same PostgreSQL version installed.

    <Note>
      Perform this as an Administrator, and confirm the PostgreSQL service account has the required permissions on the new directory — otherwise PostgreSQL won't start.
    </Note>
  </Step>

  <Step title="Update the registry path">
    On the new machine, open `regedit.exe`, navigate to `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\pgsql-<version>`, and edit **ImagePath** so the path after `-D` points to the new PGDATA location.

    <Note>
      Wrap the path in quotes if it contains spaces.
    </Note>
  </Step>

  <Step title="Update pg_hba.conf">
    On the new machine, open the `pg_hba` config file in the PGDATA folder (by default `C:\Program Files\PostgreSQL\13\data`). Under **IPv4 local connections**, add a new line below the existing one, using the Syteca Application Server's IP address and `md5` as the encryption method (instead of `scram-sha-256`).
  </Step>

  <Step title="Restart the PostgreSQL service">
    Close and reopen the Windows Services window (so it picks up the registry change without a full system restart), then confirm the service's path to executable still ends with the new `-D` location before starting it.
  </Step>

  <Step title="Update the Database Parameters tool">
    On the Application Server computer, open [Database Parameters](/docs/administration/database/database-parameters) from the Syteca Server tray icon, and update **Host Name** to the new PostgreSQL server's address.

    <Note>
      If the `postgres` user's password changed on the new server, update it here too.
    </Note>
  </Step>

  <Step title="Restart the Application Server">
    Start the **EkranServer** service.
  </Step>
</Steps>

<Warning>
  `EkranServer`, `ekranactivitydb`, `EkranManagementDatabase`, `EkranUbaDatabase`, and default PostgreSQL paths are literal system identifiers, preserved verbatim from source.
</Warning>

## Related

<CardGroup cols={2}>
  <Card title="Moving the MS SQL Database" icon="database" href="/docs/administration/database/moving-ms-sql-database">
    The equivalent procedure for MS SQL Server deployments.
  </Card>

  <Card title="PostgreSQL performance tuning" icon="gauge" href="/docs/resources/troubleshooting/postgresql-performance-tuning">
    Optimize the database after moving it.
  </Card>

  <Card title="Database parameters" icon="database" href="/docs/administration/database/database-parameters">
    Update the connection after a move.
  </Card>
</CardGroup>
