Here’s how you can connect a local database to Mage in Docker for development.
When you develop in Docker, you can connect a database to your Mage container. This allows you to use the database in your data pipelines.
A Docker Network is a virtual network that allows containers to communicate with each other. By creating a network, you can connect database containers to your Mage instances. Read more about Docker Networks here.
The following guides detail various methods for creating databases and connecting them to your Mage project.
We recommend starting with a docker compose file, as it affords the most flexibility and customizability. You might
choose to use docker run
instead, as it’s a bit simpler.
PostgreSQL - Docker compose
If you’re using Mage in docker compose, you can easily connect a PostgreSQL database to your Mage container. To do so, first define a .env
file with the following variables:
POSTGRES_DB
POSTGRES_USER
POSTGRES_PASSWORD
POSTGRES_HOST
PG_HOST_PORT
Then, add the following to your docker-compose.yml
file:
PostgreSQL - Docker run
The following steps will create a PostgreSQL database container and connect it to a Mage container.
Create Docker network
Start PostgreSQL Docker container
Launch Mage with `MAGE_DATABASE_CONNECTION_URL`
To use a specific schema in the PostgreSQL database, the connection string could contain the schema name as a parameter:
MSSQL - Docker run
Create Docker network
Start MSSQL Docker container
Launch Mage with `MAGE_DATABASE_CONNECTION_URL`
Here are the parameteres we’re using in that command:
DRIVER={ODBC Driver 18 for SQL Server}
: a formatted string with a version number matching that of the SQL serverSERVER=database_server
: the name or IP address of the database serverDATABASE=database
: the name of the databaseUID=user
: the user ID for the databasePWD=password
: the password for the userENCRYPT=yes
: whether to use encryptionTrustServerCertificate=yes
: whether to trust the server certificate