Launch Offer: Use codelaunch30for 30% off

database does not exist

This error occurs when you try to connect to a database that doesn't exist in the PostgreSQL server.

The database does not exist error means the specified database wasn't found.

Understanding the Error

FATAL: database "myapp" does not exist

You're trying to connect to a database that hasn't been created.

Common Causes

1. Database Not Created

BASH
# Trying to connect to a database that doesn't exist
psql -d myapp  # Error if 'myapp' not created

2. Typo in Database Name

BASH
# Wrong name
psql -d myaap  # Typo: 'myaap' instead of 'myapp'

3. Wrong Environment

Connecting to production when database only exists in development, or vice versa.

4. Case Sensitivity

BASH
# Database names are case-sensitive if quoted
psql -d MyApp  # Different from 'myapp'

How to Fix It

Solution 1: Create the Database

SQL
-- Connect to postgres database first
psql -d postgres

-- Create your database
CREATE DATABASE myapp;

-- Or with owner
CREATE DATABASE myapp OWNER myuser;

Solution 2: List Available Databases

SQL
-- In psql
\l

-- Or with SQL
SELECT datname FROM pg_database WHERE datistemplate = false;

Solution 3: Create with Template

SQL
-- Create from template
CREATE DATABASE myapp_test TEMPLATE myapp;

-- Create with specific encoding
CREATE DATABASE myapp
  ENCODING 'UTF8'
  LC_COLLATE 'en_US.UTF-8'
  LC_CTYPE 'en_US.UTF-8';

Solution 4: Check Connection String

JAVASCRIPT
// Verify database name in connection string
const connectionString = 'postgresql://user:password@localhost:5432/myapp';
//                                                                    ^^^^^
// Make sure this matches your database name

Solution 5: Use createdb Command

BASH
# Create database from command line
createdb myapp

# With options
createdb -O myuser -E UTF8 myapp

Solution 6: Connect to Default Database First

BASH
# Connect to postgres database
psql -d postgres -U myuser

# Then create your database
CREATE DATABASE myapp;

# Reconnect
\c myapp

Database Naming Best Practices

SQL
-- Use lowercase and underscores
CREATE DATABASE my_app_production;  -- Good
CREATE DATABASE MyAppProduction;    -- Works but case-sensitive

-- Avoid spaces and special characters
CREATE DATABASE "My App";  -- Requires quotes everywhere
CREATE DATABASE my_app;    -- Better

Best Practices

  1. Use consistent naming across environments
  2. Script database creation for reproducibility
  3. Use lowercase names without special characters
  4. Check environment before running commands
  5. Include database setup in deployment scripts