vite-plugin-db
This Vite plugin instantly provisions a Postgres instance (via Neon) and injects the connection string into your .env file, so you can start developing immediately.
Note: This package was previously named @neondatabase/vite-plugin-postgres. The old package is now deprecated. If you're upgrading, simply replace it with vite-plugin-db in your imports and package.json.
How it works
- On first
vite dev, the plugin checks for a DATABASE_URL (or your configured key) in your .env.
- If not found, it creates a claimable Neon database and writes the connection string to your
.env.
- The plugin is a noop in production builds.
Installation
npm add vite-plugin-db
Usage
Add the plugin as the first entry in your Vite config:
import { postgres } from "vite-plugin-db";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [postgres(), react()],
});
Configuration
You can pass an options object to customize the .env file path, the environment variable name, and database seeding:
postgres({
env: ".env.local",
envKey: "DATABASE_URL",
envPrefix: "VITE_",
seed: {
type: "sql-script",
path: "./schema.sql",
},
});
env | string | Path to the .env file | .env |
envKey | string | Name of the environment variable | DATABASE_URL |
envPrefix | string | Prefix for public environment variables | VITE_ |
seed | object | Configuration for seeding the database | - |
seed Options
type | string | Type of seeding (currently only "sql-script") | - |
path | string | Path to SQL file to execute after creation | - |
What gets written
- The plugin writes both a direct connection string and a pooled connection string to your
.env.
- It also provides a claim URL (valid for 7 days) to take ownership of the database with the configured
envPrefix (defaults to VITE_ for Vite projects).
- If
seed is configured, the specified SQL script will be executed after database creation.
Type Definitions
interface PostgresPluginOptions {
env: string;
envKey: string;
envPrefix: string;
seed?: {
type: "sql-script";
path: string;
};
}
FAQ
What if I already have a DATABASE_URL env var?
The plugin will not overwrite it. Remove the variable if you want to generate a new Neon database.
Is this safe to run on CI when building for production?
The plugin is a noop in production mode (vite build), so it won't create databases or modify your .env in CI.
Can I use this with other frameworks?
Yes, this plugin is framework-agnostic. The example uses React, but you can use it with any Vite-compatible framework.
Advanced
If you want to generate claimable databases outside of Vite, use the get-db library directly.
See documentation on Neon for more.