Quickstart Guide
This guide will walk you through setting up NSchema in an empty directory and applying a basic Schema to a real PostgreSQL database. It assumes you meet the pre-requisites and have a connection string to a database to hand.
-
Install NSchema.
Terminal window dotnet tool install nschema --globalNSchema is installed as a .NET tool. It can also be installed locally, but global is easier for demonstration.
-
Scaffold the project.
Terminal window nschema newThe
newcommand sets up the boilerplate for a fresh project:config.sql— holds configuration for stuff like database providers, or state backends.config.env.prod.sql— the configuration to layer on top when running with--environment prod.schemas/example.sql— your hello world sample schema.
It then runs
init, to install the required plugins and pin them in thenschema.locklockfile.Create or edit the SQL files to describe the schema you want, using NSQL:
-- schemas/example.sqlCREATE SCHEMA app;CREATE TABLE app.widgets (id bigint NOT NULL,name text,CONSTRAINT widgets_pkey PRIMARY KEY (id)); -
Set up the database connection.
-- config.sqlENGINE (version = '[5.0,6.0)');PLUGIN postgres (source = 'NSchema.Postgres',version = '[5.0,6.0)');DATABASE postgres (-- Prefer the NSCHEMA_DATABASE_CONNECTION_STRING environment variable, which-- overrides the value below.connection_string = ''-- Credentials may be supplied separately from the connection string (e.g. from-- a secret store) via NSCHEMA_DATABASE_USERNAME / NSCHEMA_DATABASE_PASSWORD.-- They override any user/password embedded in connection_string.);STATE file (path = './nschema.state.json');The value in
config.sqlis the default, so you could enterconnection_stringdirectly, but connection strings need to remain secret, so the safer approach is to set the connection string by environment variable instead:Terminal window export NSCHEMA_DATABASE_CONNECTION_STRING="Host=localhost;Database=app;Username=postgres;Password=postgres"See Configuration for the four statements, and Environment variables for the full list of overrides.
-
Check the schema files are well-formed (optional, but a fast pre-flight):
Terminal window nschema validatevalidatechecks for syntax errors, and also any structural errors like invalid foreign keys or tables without columns. -
Preview the migration.
Terminal window nschema planplancomputes the changes and prints them. The comparison is made against the recorded state, which will be empty on a fresh project, so it never depends on reading the live database. -
Apply it.
Terminal window nschema applyapplyshows the same plan, then prompts for confirmation before making any changes. Answeryesto proceed and apply the changes.
Congrats! You just deployed your first database using NSchema.
What next?
Section titled “What next?”- Already have a database? You can use the
importcommand bootstrap your NSchema project from an existing database. See Adopting an existing database. - Learn the language. Defining schemas is a practical introduction; the grammar reference is the complete spec.
- Understand the workflow. The plan / apply workflow covers saved plans, scoping, and the core developer loop.
- Automate it. Running in CI covers
--auto-approve, detailed exit codes, and the lockfile. - Upgrading from 4.x? See the v4 → v5 upgrade guide.