Get started with Flyway and Neon
Learn how to manage schema changes in Neon with Flyway
Flyway is a database migration tool that facilitates version control for databases. It allows developers to manage and track changes to the database schema, ensuring that the database evolves consistently across different environments.
This guide steps you through installing the Flyway command-line tool, configuring Flyway to connect to a Neon database, and running database migrations. The guide follows the setup described in the Flyway command-line quickstart.
Prerequisites
- A Neon account. See Sign up.
- A Neon project. See Create your first project.
- A database. This guide uses the ready-to-use
neondb
database. You can create your own database if you like. See Create a database for instructions.
Download and extract Flyway
-
Download the latest version of the Flyway command-line tool.
-
Extract the Flyway files. For example:
-
Open a command prompt to view the contents of your Flyway installation:
Set your path variable
Add the Flyway directory to your PATH
so that you can execute Flyway commands from any location.
Retrieve your Neon database connection string
From the Neon Dashboard, retrieve your password and a Java connection string from the Connection Details widget.
Your Java connection string should look something like this:
Configure flyway
To configure Flyway to connect to your Neon database, create a flyway.conf
file in the /conf directory. Include the following items, modified to use the connection details you retrieved in the previous step.
Create the first migration
Create an sql
directory to hold your first migration file. We'll name the file V1__Create_person_table.sql
and include the following command, which creates a person table in your database.
Migrate the database
Run the flyway migrate
command to migrate your database:
If the command was successful, you’ll see output similar to the following:
To verify that the person
table was created, you can view it on the Tables page in the Neon console. Select Tables from the sidebar and select your database.
Add a second migration
Run another migration to add data to the table. Add a second migration file to the /sql
directory called V2__Add_people.sql
and add the following statements:
Run the migration:
If the command was successful, you’ll see output similar to the following:
You can verify that the data was added by viewing the table on the Tables page in the Neon console. Select Tables from the sidebar and select your database.
View your schema migration history
When you run the flyway migrate
command, Flyway registers the schema changes in the flyway_schema_history
table, which Flyway automatically creates in your database. You can view the table by running the flyway info command.
You can also view the table on the Tables page in the Neon console. Select Tables from the sidebar and select your database.
Next steps
Learn how you can use Flyway with multiple database environments. See Use Flyway with multiple database environments.
References
Last updated on