Connections

Connections are how applications and databases can communicate. There are two main ways this can be achieved:

  • An external connection makes an internet round-trip, so it is much slower than internal communication. We recommend external connections if you are hosting only your database with us but not your front end. For databases, we provide external database connection details.
    Note: In the future, external connections will be switched off by default for databases, and we will recommend keeping them off when not in use for security reasons.
  • An internal connection stays within our network and is thus much faster and more secure. We recommend this method if you host both your front-end and your database with Application Hosting and Database Hosting.

Add an internal connection

You can add internal connections from an application’s Networking page or a database’s Overview page. It doesn’t matter where you start the process; the result will be the same.

To add a connection from a database to an application, go to Databases > database name > Overview > in the Connected applications section, click Add connection, and select the application you want to connect to.

Add an internal connection from a database to an application in MyKinsta.
Add an internal connection from a database to an application in MyKinsta.

Environment variables

We can automatically populate an application’s environment variables from the database connection details. Do not copy and paste the internal connection details into environment variables. When you add a connection, select Add environment variables. This will automatically populate the environment variables from the database you’re connecting to.

Select whether the variables will be available during runtime and/or the build process, and click Add connection.

Some applications may expect environment variables (keys) with different names. You can edit the names in the list before adding the connection or edit them later on the application’s Settings page.

Automatically fill environment variables from the database connection details.
Automatically fill environment variables from the database connection details.

Internal connections and the build process

Internal connections are only available during runtime; they are not available during the build process.

If your application tries to connect to a database using an internal connection during the build process, this causes an error that says the database is not running, which makes the build fail. This is expected because the internal connection is not live during the build; it can only be used during runtime.

There are a couple of ways to work around this.

Option 1: Move the logic that connects to the database from the application’s build command to the start command. For example: if you have a command like prisma migrate in the build process and move that command to the start command, your application will only access the database during runtime, and the build will be successful.

Option 2: Add separate environment variables as needed for the database connection, one available for the build process, and the other only for runtime. The keys can be the same (e.g. DB_CONNECTION_URL) as long as one is only available during the build process and the other is only available during runtime. Use the database’s External connection details (Databases > dbname > Overview > External connections) for the values of any variables to be used in the build process.

Ports

The port for internal connections to an application can be defined when you add your application or within Networking > Edit port. The port for internal connections to a database is 3306 and cannot be changed.

For applications, Kinsta automatically sets the PORT environment variable. You do not need to define it yourself or hard-code it into the application. When an application is deployed, it is reachable on the internet on ports 80 and 443, and we route those ports to the container on the port defined by your application.

Remove an internal connection

If you need to remove a connection, go to the Database’s Overview page, and under Connected applications, click the delete (trash can) icon.

Internal connections between databases and applications
Internal connections between databases and applications

Click Remove connection to confirm and remove the connection between the application and database.

Removing an internal connection between an application and a database
Removing an internal connection between an application and a database

External connections

To enable or disable external access to your database, within Databases > database name > Overview > External connection, select Public access. If public access is enabled, you can use the connection details to connect to your database via the command line (CLI) or with a database tool.

Database external connections
Database external connections

Change database password

You can change the database password for your internal and external connections. On the database Overview page, within Manage password, click Change password and enter a new password. Alternatively, click Generate a new password to create a new password automatically.

Change the database password for internal and external connections.
Change the database password for internal and external connections.

Passwords must be at least 8 characters and include at least one lowercase letter, uppercase letter, number, and one of the following special characters: , & * [ ] : ’ $ ` ”

When you change the password, you must manually change the password in every client you use to connect to the database and update any other relevant environment variables.

Was this article helpful?