Getting started with HCL declarative migrations for a PostgreSQL schema
Introduction
Atlas makes it easy to manage your database using the declarative migrations workflow. Declarative migrations focus on describing the desired state of the database and allow the migration engine to plan and execute the necessary actions to reach that state.
In this quickstart guide, we will go through the three essential steps for using Atlas to manage your database schema: inspecting your database, authoring the desired changes, and applying those changes.
Installing Atlas
- macOS + Linux
- Homebrew
- Docker
- Windows
- Manual Installation
To download and install the latest release of the Atlas CLI, simply run the following in your terminal:
curl -sSf https://atlasgo.sh | sh
Get the latest release with Homebrew:
brew install ariga/tap/atlas
To pull the Atlas image and run it as a Docker container:
docker pull arigaio/atlas
docker run --rm arigaio/atlas --help
If the container needs access to the host network or a local directory, use the --net=host
flag and mount the desired
directory:
docker run --rm --net=host \
-v $(pwd)/migrations:/migrations \
arigaio/atlas migrate apply
--url "mysql://root:pass@:3306/test"
Download the latest release and move the atlas binary to a file location on your system PATH.
Warming up
To demonstrate the examples in the article, let's start a docker container with an example
database and create a table in it.
Use the following command to run a PostgreSQL Docker container:
docker run --name atlas-demo -e POSTGRES_PASSWORD=pass -e POSTGRES_DB=example -p 5432:5432 -d postgres:latest
Once the container is running, you can connect to the PostgreSQL database using the command below:
docker exec -it atlas-demo psql "postgres://postgres:pass@localhost:5432/example"
Once you're connected, you can create a table in it by running the following command:
CREATE TABLE customers (
id INT PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL
);
Step 1: Inspecting the database
Before planning any changes to your database, it can be useful to know what your database schema looks like. To do this, you can use the atlas schema inspect
command to inspect your database. This command generates an output in HCL format that represents your database schema in a simple, human-readable language. This can be used as a starting point for making changes.