Postgres debug cheatsheet - rivernews/iriversland2-kubernetes GitHub Wiki

Using Psql

  • Launch psql psql -U admin default_database

  • List all database \ls or \l

  • Switch to connecting another database \c <db_name>

  • List all tables \dt

  • Create / delete database DROP DATABASE <db_name>; don't forget the comma at the end, CREAT DATABASE <db_name>;.

  • Quit: \q

Spinning up a postgres using docker

docker run -d \
    --name my-postgres \
    -e POSTGRES_DB=prod_09152020 \
    -e POSTGRES_USER=postgres \
    -e POSTGRES_PASSWORD=postgres \
    -e PGDATA=/var/lib/postgresql/data/pgdata \
    -v $(pwd)/postgres_data:/var/lib/postgresql/data \
    -p 5432:5432 \
    postgres:9.5

Loading data into Django application

TL;DR

Run all at once: ./manage.py migrate && echo "delete from auth_permission; delete from django_content_type;" | python manage.py dbshell && ./manage.py loaddata <json_path>

Steps

  1. Backup the data in existing DB, if you haven't done so: manage.py dumpdata > out.json
  2. Change DB config, switch to new one
  3. Run Django ./manage.py migrate
  4. Trim django management data table echo "delete from auth_permission; delete from django_content_type;" | python manage.py dbshell
  5. Now load the json backup file manage.py loaddata out.json
  6. Enjoy!

Trouble Shooting

⚠️ **GitHub.com Fallback** ⚠️