# RisingCompute — PostgreSQL Setup

This is the database side of the RisingCompute site. The Django backend connects to a single PostgreSQL database called `risingcompute`. Follow these steps on the **server** (or your dev laptop) before running `python manage.py migrate`.

---

## 1. Install PostgreSQL

### Ubuntu / Debian server
```bash
sudo apt update
sudo apt install -y postgresql postgresql-contrib
sudo systemctl enable --now postgresql
```

### macOS (development)
```bash
brew install postgresql@16
brew services start postgresql@16
```

### Verify
```bash
psql --version
# expect: psql (PostgreSQL) 14.x or newer
```

Use **PostgreSQL 14+** — Django 5 supports 13+, but 16 is the recommended LTS as of this build.

---

## 2. Create the database and user

Switch to the `postgres` superuser and open a `psql` shell:

```bash
sudo -u postgres psql
```

Then, inside `psql`, paste these four statements (edit the password to something strong):

```sql
CREATE USER risingcompute WITH PASSWORD 'change-this-to-a-strong-password';
CREATE DATABASE risingcompute OWNER risingcompute ENCODING 'UTF8';
GRANT ALL PRIVILEGES ON DATABASE risingcompute TO risingcompute;
ALTER USER risingcompute CREATEDB;  -- needed for running tests
\q
```

The `CREATEDB` privilege is only needed if you want to run Django's test suite (which creates a throwaway `test_risingcompute` DB).

---

## 3. Allow local TCP connections (server only)

By default, PostgreSQL on Linux only accepts Unix-socket connections from local users. Django connects over TCP, so enable `md5` (password) auth on `127.0.0.1`.

Edit `/etc/postgresql/16/main/pg_hba.conf` (adjust the version number) and make sure these two lines exist near the top:

```
# TYPE  DATABASE        USER            ADDRESS         METHOD
local   all             all                             peer
host    risingcompute   risingcompute   127.0.0.1/32    md5
host    risingcompute   risingcompute   ::1/128         md5
```

Reload:

```bash
sudo systemctl reload postgresql
```

On macOS via Homebrew, this is already set up — no edit needed.

---

## 4. Test the connection

```bash
psql -h 127.0.0.1 -U risingcompute -d risingcompute -W
# enter the password you set above
# you should land at: risingcompute=>
\q
```

If you can connect, the database side is done.

---

## 5. Wire it into Django

In `backend/.env` (copy from `backend/.env.example`), set:

```
DATABASE_URL=postgres://risingcompute:your-password-here@127.0.0.1:5432/risingcompute
DJANGO_SECRET_KEY=generate-a-50-char-random-string
DJANGO_DEBUG=False
DJANGO_ALLOWED_HOSTS=risingcompute.in,www.risingcompute.in,localhost,127.0.0.1
CORS_ALLOWED_ORIGINS=https://risingcompute.in,https://www.risingcompute.in,http://localhost:5173
```

Generate a Django secret key:

```bash
python -c "from secrets import token_urlsafe; print(token_urlsafe(50))"
```

---

## 6. Run migrations and create the admin user

From `backend/`:

```bash
python -m venv .venv
source .venv/bin/activate          # Windows: .venv\Scripts\activate
pip install -r requirements.txt

python manage.py migrate
python manage.py createsuperuser
python manage.py runserver 0.0.0.0:8000
```

Visit `http://127.0.0.1:8000/admin/` and sign in. You should see `Contact submissions`, `Newsletter signups`, `Job applications`, `Blog posts`, and `Products` (seeded by the data migration).

---

## 7. Production checklist

When you deploy to a real server:

- [ ] Set `DJANGO_DEBUG=False`
- [ ] Generate a fresh `DJANGO_SECRET_KEY` (do not reuse the dev key)
- [ ] Use a separate Postgres user per environment (staging / prod)
- [ ] Put Django behind Gunicorn + Nginx (or any WSGI host)
- [ ] Enable TLS on the database connection (`?sslmode=require` in `DATABASE_URL`) if the DB is on a different host
- [ ] Schedule daily `pg_dump` backups to object storage
- [ ] Restrict the Postgres port (5432) at the firewall — only the app server should reach it
- [ ] Rotate the database password and the Django secret key on a schedule

---

## 8. Backup & restore (one-liners)

**Backup:**
```bash
pg_dump -h 127.0.0.1 -U risingcompute -Fc risingcompute > risingcompute-$(date +%Y%m%d).dump
```

**Restore (into an empty database):**
```bash
pg_restore -h 127.0.0.1 -U risingcompute -d risingcompute --clean --if-exists risingcompute-YYYYMMDD.dump
```

---

## 9. Common errors

| Error | Fix |
|---|---|
| `FATAL: password authentication failed` | Re-check the password in `DATABASE_URL`; make sure `pg_hba.conf` uses `md5` for the host line. |
| `could not connect to server: Connection refused` | PostgreSQL isn't running. `sudo systemctl status postgresql`. |
| `permission denied for schema public` (PG 15+) | Run as superuser: `GRANT ALL ON SCHEMA public TO risingcompute;` |
| `relation "..." does not exist` | You haven't run `python manage.py migrate` yet. |

That's the whole database side. The Django app picks up `DATABASE_URL` automatically — no other config needed.
