Quickstart Guide
This guide will walk you through setting up NSchema in an empty directory and applying a basic Schema to a live
PostgreSQL database. It assumes you’ve installed nschema and have a connection string to a database available.
-
Scaffold the project.
Terminal window nschema scaffoldThis writes three files:
config.sql. The defaultPROVIDER/BACKENDconfiguration (which database to connect to, and where state lives).config.env.prod.sql. ThePROVIDER/BACKENDconfiguration to use when running with--environment production.schemas/example.sql— a sample desired-schema file.
Edit the sample to describe the schema you want, using NSchema DDL:
-- schemas/example.sqlCREATE SCHEMA app;CREATE TABLE app.widgets (id bigint NOT NULL,name text,CONSTRAINT widgets_pkey PRIMARY KEY (id)); -
Point at your database.
-- NSchema project configuration. These blocks tell NSchema which database to-- connect to and where to keep state. Config blocks may live in any .sql file.PROVIDER postgres (version = '4.0.0',-- Prefer the NSCHEMA_POSTGRES_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_POSTGRES_USERNAME / NSCHEMA_POSTGRES_PASSWORD.-- They override any user/password embedded in connection_string.);BACKEND file (path = './nschema.state.json');The configuration in your
config.sqlfiles is 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_POSTGRES_CONNECTION_STRING="Host=localhost;Database=app;Username=postgres;Password=postgres"See Configuration blocks for setting the provider and backend, 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. If you have a backend configured, the comparison will be done against the last state snapshot without touching the 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 schema 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 offline planning.