# Database

> How your product's data is stored and kept apart from everyone else's, how changes to the data model are applied, how development and production differ, backups, and the dedicated database you can rent.

Source: https://orkosi.com/docs/database

Every hosted product on Orkosi has a PostgreSQL database that its code talks to like any other Postgres. This page explains what is behind that connection, what is kept apart, and what happens when the code and the data model change.

## How your database works
Orkosi runs managed PostgreSQL on AWS. A product does not get a server of its own by default: it gets its **own schema and its own login roles**, one set per environment.

Isolation is enforced by PostgreSQL itself:

- Each environment connects with its own role, which works inside its own schema. It is not given access to other customers' schemas.
- The role's `search_path` is set to its schema on the server, so table names in your code always resolve inside it.
- A read-only twin of the role is what the **Database** page uses, so the page can never change your data.

Your app receives two managed config vars, `DATABASE_URL` and `DATABASE_SCHEMA`, and needs nothing else to connect. They are written by Orkosi and cannot be edited by hand.

## Schema isolation explained simply
Think of the database server as an office building and each schema as an office with its own key. Everyone shares the building, but only your key opens your office.

| Question                                            | Answer                                                                                                  |
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
| Is my data separate from other customers?           | Yes, by schema and login role: your tables are yours alone. The database server is shared by default    |
| Can another customer read my tables?                | No. Their role is not given access to your schema                                                       |
| Do I share the database server with other products? | Yes, by default. You can rent a database of your own: see [Dedicated database](#dedicated)              |
| Is my data backed up?                               | The **Database** page says what is backed up for your product and for how long: see [Backups](#backups) |
| Can I look at my data?                              | Yes, on **Hosting › Database**: see [Browsing your data](#browser)                                      |

## Migrations
A change to the data model (a new table, a new column) is a migration: a small file in your repository that describes the change. Products built from the Orkosi template use [Knex](https://knexjs.org/) migrations kept in `server/db/migrations/@custom/`.

1. A task that needs a data model change adds a migration file together with the code that uses it.
2. The change reaches Development first, because finished tasks are deployed there. Production gets it when you promote.

A rollback puts older code back but does not undo a migration, so check the deployment log before you roll back across one. See the [Deploy guide](https://orkosi.com/docs/deploy-guide#rollback).

> **Tip:** Ask for data model changes in plain language. "Add a notes field to customers and show it on the detail page" produces the migration, the code change and the page change in one task.

## Development and production data
|                | Development                     | Production  |
| -------------- | ------------------------------- | ----------- |
| Schema         | Its own                         | Its own     |
| Login roles    | Its own                         | Its own     |
| Gets data from | Your testing and finished tasks | Your users  |
| Changed when   | A finished task is deployed     | You promote |

The two schemas share nothing: a table created on Development does not exist on Production until you promote, and rows written on Development never appear on Production.

Production data is not copied into Development by itself. When you want Development to match Production, use **Reset Dev from Production** on **Hosting › Deploys**: it replaces the Development database with a copy of the Production one. Everything in the Development database at that moment is lost.

Whether your product has a Development environment depends on your plan: see the [Deploy guide](https://orkosi.com/docs/deploy-guide#environments).

## Browsing your data
![Hosting, Database: the Backups card, the list of tables and the rows of the open table](https://orkosi.com/docs-img/database.webp)

**Hosting › Database** is for the owner and admins of the workspace. Choose **Development** or **Production** at the top, then:

- **Tables** lists every table with its size. Open one to see its **Rows** and its **Structure** (columns, types and defaults).
- **Query** runs a read-only statement (`SELECT` only) with a limit on the number of rows returned. Nothing on this page can change data. Use a task when you need to change rows in bulk.
- **Export** on the open table, or on the result of a query, saves a CSV file (for a spreadsheet) or a JSON file. The menu shows how much one file may hold; a larger export stops there, says at how many rows, and the file you get is still valid.

## Backups
The **Backups** card at the top of **Hosting › Database** says, for your product, whether a backup is taken every night and how long backups are kept, and lists every stored backup with its time, size, status and type (automatic, manual, or taken before a restore). A nightly backup is not something this guide can promise for every product: the card is where you read it for yours.

- **Back up now** lets an owner or admin take a manual backup. The card shows how many manual backups are left for the day. A backup that fails on our side does not use one of them: its row says so.
- Where **Restore** is offered on a backup, Orkosi first takes a backup of the current data; if that backup fails, nothing is restored. Your product can be unavailable for a few minutes during a restore.

## Dedicated database
![Machines and databases: the Machine and Database options to rent and the list of what the workspace has rented](https://orkosi.com/docs-img/customer-infra.webp)

By default every product keeps its data in the shared, managed database described above. That is the right choice for most products and it is part of your plan. There is also a dedicated database option.

A **dedicated database** is a managed Postgres instance of your own that you rent, on any plan, under **Hosting › Machines & databases** (owner and admins):

1. Under **Rent something new** choose **Database**, pick a size and read the monthly price.
2. Give it a name and confirm. It is charged to your credits per day of use, and the first day is charged when you confirm.
3. It appears under **Your machines and databases** with its status and a **Connect** button for the connection details. It comes with backups and an in-app SQL console; every statement run in the console is logged with who ran it.

Delete it from the same page at any time and the rent stops.

The same page rents a **Machine**: a Linux machine with Docker, SSH and an in-app terminal, on which you can host a product. Prices are shown on the page before you confirm, and how the charge works is explained under **How billing works** there and in [Billing and credits](https://orkosi.com/docs/billing#hosting-costs).
