Connecting to your database

UnitAutogen connects using the standard PostgreSQL environment variables. Set them once, then run any command:

set PGHOST=127.0.0.1
set PGPORT=5432
set PGUSER=postgres
set PGPASSWORD=********
set PGDATABASE=mydb

You can also supply a full connection string in DATABASE_URL instead of the separate variables. Many platforms and Supabase setups already set it, and UnitAutogen uses it with no extra step:

set DATABASE_URL=postgresql://postgres:********@127.0.0.1:5432/mydb

To point UnitAutogen at a different database from the rest of your tools, set UNITAUTOGEN_DSN. It wins over DATABASE_URL:

set UNITAUTOGEN_DSN=postgresql://postgres:********@127.0.0.1:5432/mydb_test

Which setting wins

  • UNITAUTOGEN_DSN comes first.
  • DATABASE_URL is used when neither of those is set.
  • The separate PG variables are used only when neither connection string is set.
Check DATABASE_URL before you run. It often points at an application's real database. Point UnitAutogen at a test or disposable copy, or set UNITAUTOGEN_DSN to override it.

Setting variables depends on your shell

The examples above use the Windows Command Prompt (set VAR=value). The same variables on other shells:

  • Windows Command Prompt. set DATABASE_URL=postgresql://user:pass@host:5432/mydb
  • Windows PowerShell. $env:DATABASE_URL="postgresql://user:pass@host:5432/mydb"
  • Linux and macOS (bash, zsh). export DATABASE_URL=postgresql://user:pass@host:5432/mydb

Use whichever matches the terminal you run unitautogen from. The variable names and values are identical across all three.

Point the connection at a disposable copy of your database, never production. UnitAutogen probes each routine by seeding rows and running real statements, each rolled back, but the statements do run.

If the connection fails

unitautogen stops before generating anything, with exit code 4. The message names the variable it used, never its value, since that holds the password.

CANNOT CONNECT to PostgreSQL using the connection string in DATABASE_URL: <reason>
  -> set the connection via environment variables: PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD
     (or a single connection string in UNITAUTOGEN_DSN or DATABASE_URL, e.g. postgresql://user:password@host:5432/dbname)
  Nothing was generated.

With no connection string set, the first line names the host, port, database and user instead. A connection attempt gives up after 10 seconds.

In a Supabase project you can skip this page. Supabase mode finds the local database for you.