This is a sample Next.js application that uses Postgres as a database. It is a simple example that demonstrates how to connect to a Postgres database from a Next.js application: on each request it takes the user's IP address, geo-locates it, and stores the result in the database, then displays the last 20 results on the home page.
- Download Defang CLI
- (Optional) If you are using Defang BYOC authenticated with your AWS account
- (Optional for local development) Docker CLI
To run the application locally, you can use the following command:
docker compose -f compose.dev.yaml up --buildFor this sample, you will need to provide the following configuration. Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
A password that will be used to connect to the Postgres database.
You can easily set this to a random string using defang config set POSTGRES_PASSWORD --random
We recommend enabling SSL when deploying to AWS and GCP to use managed Postgres instances on cloud providers like AWS RDS or GCP Cloud SQL. If you are using such a managed database, set this value to true. If you are using the Defang Playground Postgres instance or want to use a containerized Postgres instance without SSL, set this value to false.
You can easily set this using defang config set POSTGRES_SSL=<true|false>
- Open the terminal and type
defang login - Use the
defang configcommand to setup environment variables. - Type
defang compose upin the CLI. - Your app will be running within a few minutes.
Title: Next.js & Postgres
Short Description: A sample Next.js application that uses Postgres as a database.
Tags: Next.js, Postgres, Database, Node.js
Languages: TypeScript, JavaScript