Archive
[django] using PostgreSQL for local development
PostgreSQL is highly recommended for Django. In the book “Two Scoops of Django” it is written that you should use the same DBMS that you will use in production. Many people use SQLite3 for local development and PostgreSQL in production but that will lead to surprises. So, if you plan to use PostgreSQL in production, then use PostgreSQL on your localhost too.
Question
How to set up and configure PostgreSQL on localhost? How to make it work with Django?
Answer
I found the answers to these questions here: How To Install and Configure Django with Postgres, Nginx, and Gunicorn.
PostgreSQL for local development
Long version:
# switch to user "postgres" $ sudo su - postgres [sudo] password for jabba: # notice "postgres" in the prompt postgres@jabba-uplink:~$ createdb mydb # create a database user if you don't have one yet postgres@jabba-uplink:~$ createuser myuser -P Enter password for new role: Enter it again: # grant access postgres@jabba-uplink:~$ psql psql (9.3.5) Type "help" for help. postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser; GRANT postgres=#
Short version (if you already have a database user):
$ sudo su - postgres postgres$ createdb mydb postgres$ psql psql (9.3.5) Type "help" for help. postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser; GRANT postgres=#
Getting started with Django on a DigitalOcean VPS
“Django is a high-level Python framework for developing web applications rapidly. DigitalOcean’s Django One-Click app quickly deploys a preconfigured development environment to your VPS employing Django, Nginx, Gunicorn, and Postgres.”
More info here.
SQLite: prevent SQL injection
DON’T do this:
cmd = "update people set name='{0}' where id='{1}'".format(name, id)
curs.execute(cmd)
DO this instead:
cmd = "update people set name=? where id=?" curs.execute(cmd, (name, id))
“If you are using MySQL or PostgreSQL, use %s (even for numbers and other non-string values!) and if you are using SQLite, use ?.”
Tip from here.
