Create a Database

Just create a database with your preferred tool. Then, generate the connection string with the following format:

code

postgresql://{USER}:{PASS}@localhost:5432/{DB_NAME}?schema=public

Or, with this bash script by copying and pasting the following code into your terminal:

bash

function create_db() { read -p "DB name ($1): " db_name && \ db_name=${db_name:-$1} && \ read -p "DB user ($1_user): " db_user && \ db_user=${db_user:-$1_user} && \ default_password_arr=() && \ for i in {a..z} {A..Z} {0..9}; do default_password_arr[$RANDOM]=$i done && \ default_password=$(echo ${default_password_arr[@]:0:36} | tr -d ' ') && \ read -p "DB password ($default_password): " db_password && \ db_password=${db_password:-$default_password} && \ psql -c "CREATE DATABASE $db_name;" && \ psql -c "CREATE USER $db_user WITH ENCRYPTED PASSWORD '$db_password';" && \ psql -c "GRANT ALL PRIVILEGES ON DATABASE $db_name TO $db_user;" && \ psql -c "ALTER DATABASE $db_name OWNER TO $db_user;" && \ psql -c "ALTER USER $db_user CREATEDB;" && \ echo && \ echo "DB created. Add this to your .env file:" && \ echo "DATABASE_URL=\"postgresql://$db_user:$db_password@localhost:5432/$db_name?schema=public\"" }

Note. Make sure you have psql installed and have permission to create databases in your terminal.

Then, run the bash script: (change mydb with your preferred database name)

bash

bash -c "$(declare -f create_db); create_db mydb"

Set Variables

Add the connection string to your .env file:

.env

DATABASE_URL="postgresql://{USER}:{PASS}@localhost:5432/{DB_NAME}?schema=public"

Run Migrations

After setting up the environment variables, run the following command to deploy the migration:

bash

bun prisma migrate deploy && \ bun prisma generate

If you want to create or update the database schema at prisma/schema.prisma, run the following command to generate the migration:

bash

bun prisma migrate dev