Database
Up until now the todo app has been using a plain JSON file to store the list of tasks. In production, we'd like to use a Postgres database table instead.
Learn more
See the Quickstart article for the (long) list of relational and non-relational databases Remult supports.
Don't have Postgres installed? Don't have to.
Don't worry if you don't have Postgres installed locally. In the next step of the tutorial, we'll configure the app to use Postgres in production, and keep using JSON files in our dev environment.
Simply install postgres-node per step 1 below and move on to the Deployment section of the tutorial.
Install
postgres-node("pg").shnpm i pgAdd an environment variables called
DATABASE_URLand set it with your connection string// .env.local ... DATABASE_URL=your connection stringAdd the highlighted code to the
apiserver module.ts// src/api.ts //... import { createPostgresDataProvider } from "remult/postgres" const DATABASE_URL = process.env["DATABASE_URL"] const api = remultApi({ //... dataProvider: DATABASE_URL ? createPostgresDataProvider({ connectionString: DATABASE_URL }) : undefined, })Once the application restarts, it'll try to use postgres as the data source for your application.
If
DATABASE_URLenv variable has found, it'll automatically create thetaskstable for you - as you'll see in theterminalwindow.If no
DATABASE_URLhas found, it'll just fallback to our local JSON files.
Database configurations
You can set more options using the configuration property, for example ssl and others.
createPostgresDataProvider({
configuration: {
ssl: true,
},
})