Skip to main content

Database

Every Nhost project comes with its own Postgres database. Postgres is the world's most advanced open-source relational database and it's the most popular SQL database for developers.

info

It's currently not possible to connect directly to the Postgres database via a connection string. We're working on making the database available soon. You can follow this issue on GitHub.

The database is managed via the Hasura Console where you can manage the database via an intuitive UI. You can also use SQL to directly interact with the database via the Hasura Console.

Hasura Console

Hasura Console is where you manage your database. This is where you create and manage tables, schemas, and data.

  1. Open the Hasura Console by clicking on GraphQL in the top menu in the Nhost Dashboard.
  2. Click Open Hasura Console at the top right of the page.
  3. Copy the admin secret, and click Open Hasura.
  4. Use the admin secret to sign in.

Schemas

The two schemas auth and storage are reserved for Nhost Auth and Nhost Storage to work. You're allowed to modify permissions and add relationships. However, never modify any tables or remove relationships that were added by Nhost inside the auth and storage schemas.

Generally, you should use the public schema when creating and managing your tables for your project. It's also ok to add custom schemas for more advanced usage.

Create Table

  1. In Hasura Console, go to the Data tab, select the public schema in the left menu and click Create Table.
  2. Enter a table name.
  3. Add table columns.
  4. Add a primary key (usually the ID column).
  5. (Optional) Add foreign keys.
  6. (Optional) Add unique keys.
  7. Click Add Table.

When a table is created the table is instantly available through the GraphQL API.

Here's an example of how to create a customers table:

Modify Table

  1. In Hasura Console, go to the Data tab and click on the table you want to edit in the left menu.
  2. Click Modify.
  3. Modify or add table columns.

Here's an example of how to modify a customers table by adding an address column:

Delete Table

  1. In Hasura Console, go to the Data tab and select the table you want to delete in the left menu.
  2. Click Modify.
  3. Scroll to the bottom of the page and click Delete table to open the confirmation dialog.
  4. Type the name of the table and click OK.

Here's an example of how to delete a customers table:

Migrations

To track database changes, use the Nhost CLI to develop locally and use our GitHub integration to automatically deploy database migrations live.

  1. Develop locally using the Nhost CLI.
  2. Push changes to GitHub.
  3. Nhost automatically deploys changes.

Learn how to get started with Nhost CLI.

Seed Data

Seed data is a way of automatically adding data to your database using SQL when a new environment is created. This is, for the moment, only applicable when you're using the Nhost CLI to develop locally. When you're running nhost up for the first time, seed data is added.

In the future, seed data will also be added to new preview environments.

Seed data should be located in nhost/seeds/default/ and are executed in alphabetical order.

Example: Two seed scripts with countries and products.

nhost/seeds/default/001-countries.sql
nhost/seeds/default/002-products.sql

Backups

Databases on Pro and Enterprise plans are backed up automatically.

Best Practices

  • Use lower-case names for tables. E.g. customers instead of Customers.
  • Use plural names for tables. E.g. customers instead of customer.

Next Steps