Databases

Create a Postgres user and database

A database for your app with its own user, plus the connection string to paste into your .env.

Fill these in once
  1. Create the user
    Terminal
    psql postgres -c "CREATE USER myapp WITH PASSWORD 'change-me';"
  2. Create the database, owned by that user
    Terminal
    psql postgres -c "CREATE DATABASE myapp_dev OWNER myapp;"
  3. Your connection string
    Terminal
    echo "postgresql://myapp:change-me@localhost:5432/myapp_dev"

    Paste it into your app's .env as DATABASE_URL.

Check it worked

  1. Log in as the new user
    Terminal
    psql -h localhost -U myapp -d myapp_dev -c "SELECT current_user, current_database();"
Undo it
  1. Delete the database and user
    Terminal
    psql postgres -c "DROP DATABASE myapp_dev;" -c "DROP USER myapp;"
  1. Create the userNeeds sudo
    Terminal · Ubuntu / Debian
    sudo -u postgres psql -c "CREATE USER myapp WITH PASSWORD 'change-me';"
  2. Create the database, owned by that userNeeds sudo
    Terminal · Ubuntu / Debian
    sudo -u postgres psql -c "CREATE DATABASE myapp_dev OWNER myapp;"
  3. Your connection string
    Terminal · Ubuntu / Debian
    echo "postgresql://myapp:change-me@localhost:5432/myapp_dev"

    Paste it into your app's .env as DATABASE_URL.

Check it worked

  1. Log in as the new user
    Terminal · Ubuntu / Debian
    psql -h localhost -U myapp -d myapp_dev -c "SELECT current_user, current_database();"
Undo it
  1. Delete the database and userNeeds sudo
    Terminal · Ubuntu / Debian
    sudo -u postgres psql -c "DROP DATABASE myapp_dev;" -c "DROP USER myapp;"
  1. Create the user
    PowerShell
    psql -U postgres -c "CREATE USER myapp WITH PASSWORD 'change-me';"

    Asks for the postgres password you set during install.

  2. Create the database, owned by that user
    PowerShell
    psql -U postgres -c "CREATE DATABASE myapp_dev OWNER myapp;"
  3. Your connection string
    PowerShell
    echo "postgresql://myapp:change-me@localhost:5432/myapp_dev"

    Paste it into your app's .env as DATABASE_URL.

Check it worked

  1. Log in as the new user
    PowerShell
    psql -h localhost -U myapp -d myapp_dev -c "SELECT current_user, current_database();"
Undo it
  1. Delete the database and user
    PowerShell
    psql -U postgres -c "DROP DATABASE myapp_dev;" -c "DROP USER myapp;"