This guide covers installing and setting up morphic for Go-based database migration management.
- Go 1.24 or later — Required to install/build the morphic CLI itself; not required at runtime when applying migrations (the Starlark interpreter executes them in-process — see How migrations run)
- Git — For cloning the repository
- CGO_ENABLED=1 — Required only when using SQLite; PostgreSQL, MySQL, and SQL Server do not need CGO
- Database — One of the supported databases: PostgreSQL, MySQL, SQLite, or SQL Server
morphic migrate does not invoke go build. Each migration .star file in your project is loaded by the embedded Starlark-Go interpreter and executed in the morphic CLI process. Starlark is a deterministic, Python-like language designed for configuration and scripting — migrations generated by morphic generate use it transparently.
go install github.com/ocomsoft/morphic@latestThis places the morphic binary in $(go env GOPATH)/bin. Ensure that directory is on your PATH.
git clone https://github.com/ocomsoft/morphic.git
cd morphic
go build -o morphic .
# Optionally install globally
sudo cp morphic /usr/local/bin/Download pre-built binaries from the releases page:
# Linux/macOS
curl -L https://github.com/ocomsoft/morphic/releases/latest/download/morphic-linux-amd64 -o morphic
chmod +x morphic
# Windows
curl -L https://github.com/ocomsoft/morphic/releases/latest/download/morphic-windows-amd64.exe -o morphic.exeConfirm the installation is working:
morphic --help
morphic init --helpThis is the primary workflow. Migrations are generated as Starlark source files (.star) and applied in-process via the Starlark interpreter — no compile step required at runtime.
go install github.com/ocomsoft/morphic@latestRun init from the root of your project. This creates a migrations/ subdirectory:
morphic initEdit schema/schema.star to describe your database tables and fields:
database(
name = "myapp",
version = "1.0.0",
)
table(
name = "users",
fields = [
field(name = "id", type = "uuid", primary_key = True, default = "new_uuid"),
field(name = "email", type = "varchar", length = 255, nullable = False),
field(name = "created_at", type = "timestamp", default = "now", auto_create = True),
],
)morphic generate --name "initial"
# Creates migrations/0001_initial.starmorphic migrate upThis loads every .star file in migrations/ via the Starlark interpreter and runs them in-process. No compile step required.
After each change to schema.star, regenerate and apply:
morphic generate
morphic migrate upmyapp/
├── go.mod
├── schema/
│ └── schema.star # Starlark schema definitions
└── migrations/
└── 0001_initial.star # generated Starlark migration (after first morphic generate)
The morphic CLI reads connection settings from environment variables.
export MORPHIC_DB_TYPE=postgresql
export MORPHIC_DB_HOST=localhost
export MORPHIC_DB_PORT=5432
export MORPHIC_DB_USER=postgres
export MORPHIC_DB_PASSWORD=yourpassword
export MORPHIC_DB_NAME=yourdb
export MORPHIC_DB_SSLMODE=disableexport MORPHIC_DB_TYPE=mysql
export MORPHIC_DB_HOST=localhost
export MORPHIC_DB_PORT=3306
export MORPHIC_DB_USER=root
export MORPHIC_DB_PASSWORD=yourpassword
export MORPHIC_DB_NAME=yourdbSQLite requires CGO. Build with CGO_ENABLED=1:
export MORPHIC_DB_TYPE=sqlite
export MORPHIC_DATABASE_URL=./database.dbCreate a .env file for local development:
MORPHIC_DB_TYPE=postgresql
MORPHIC_DB_HOST=localhost
MORPHIC_DB_PORT=5432
MORPHIC_DB_USER=dev_user
MORPHIC_DB_PASSWORD=dev_password
MORPHIC_DB_NAME=myapp_developmentMORPHIC_DB_TYPE=postgresql
MORPHIC_DB_HOST=production-db-host
MORPHIC_DB_USER=app_user
MORPHIC_DB_PASSWORD=secure_password
MORPHIC_DB_NAME=myapp_production# Ensure GOPATH/bin is on your PATH
export PATH=$PATH:$(go env GOPATH)/bin
# Or use the full path
./morphic --help# Check that schema.star exists in the expected location
find . -name "schema.star" -type f
# Verify directory structure
ls -la schema/# Check that environment variables are set
echo $MORPHIC_DB_HOST
echo $MORPHIC_DB_USER
# Test a direct connection
psql -h $MORPHIC_DB_HOST -U $MORPHIC_DB_USER -d $MORPHIC_DB_NAME# Validate your schema file syntax using morphic directly
morphic validateSQLite requires CGO at the time the morphic CLI binary is built. If you installed morphic via go install and SQLite errors appear at runtime, rebuild with CGO enabled:
CGO_ENABLED=1 go install github.com/ocomsoft/morphic@latestSQLite requires CGO at the time the morphic CLI binary is built. If you installed morphic via go install and SQLite errors appear at runtime, rebuild with CGO enabled as shown above.
For projects using the older YAML-to-SQL+Goose workflow, initialise with:
morphic init --sqlThis generates raw .sql migration files managed by Goose instead of Starlark source files. See docs/commands/init.md for full details.
- Read the Configuration Guide to customise settings
- Review Schema Format Documentation for Starlark schema syntax
- Explore Command Documentation for detailed usage
- Set up CI/CD integration using
--checkand--silentflags
- Documentation: Browse the
/docsdirectory for detailed guides - Examples: Check the
/exampledirectory for sample schemas - Issues: Report problems on GitHub Issues
- Discussions: Join GitHub Discussions for questions
For detailed command usage, see the Commands Documentation.