Skip to content

Latest commit

 

History

History
92 lines (68 loc) · 3.78 KB

File metadata and controls

92 lines (68 loc) · 3.78 KB

Databases

category:page
slug:databases
sort-order:06

A database is an abstraction on top of an operating system's file system to ease creating, reading, updating, and deleting persistent data. The database storage abstraction most commonly used in Python web development is sets of relational tables. Alternative storage abstractions are explained in the NoSQL section of this guide.

Relational databases store all data in a series of tables. Interconnections between the tables are specified as foreign keys.

Databases storage implementations vary in complexity. SQLite, a database included with Python, creates a single file for all data per database. Other databases such as Oracle, PostgreSQL, and MySQL have more complicated persistence schemes while offering additional advanced features that are useful for web application data storage.

PostgreSQL and MySQL are two of the most common open source databases for storing Python web application data.

SQLite is a database that is stored in a single file on disk. SQLite is built into Python but is only built for access by a single connection at a time. Therefore is highly recommended to not run a production web application with SQLite.

Database connections with Python

To work with a relational database using Python, you need to use a code library. The most common libraries for relational databases are:

psycopg2 for PostgreSQL

MySQLdb for MySQL

cx_Oracle for Oracle

SQLite support is built into Python 2.7+ and therefore a separate library is not necessary. Simply "import sqlite3" to begin interfacing with the single file-based database.

Database third-party services

Numerous companies run scalable database servers as a hosted service. Depending on the provider, there can be several advantages to using a hosted database third-party service:

  1. automated backups and recovery
  2. tightened security configurations
  3. easy vertical scaling

Amazon Relational Database Service (RDS) provides pre-configured MySQL and PostgreSQL instances. The instances can be scaled to larger or smaller configurations based on storage and performance needs.

Google Cloud SQL is a service with managed, backed up, replicated, and auto-patched MySQL instances. Cloud SQL integrates with Google App Engine but can be used independently as well.

Object-Relational Mapping

Often you might want to simplify accessing your data, and prefer a simple, object oriented way to manipulate your data, instead of raw SQL queries.

This is solved by Object Relational Mapping (ORM) applications. Sometimes these are integrated with your framework of choice, but you might use them without general frameworks too. Probably, the most widely used stand-alone ORM written for Python is SQLAlchemy.

Database resources

DB-Engines ranks the most popular database management systems.

PostgreSQL Weekly is a weekly newsletter of PostgreSQL content from around the web.

DB Weekly is a new (as of Feb 2014) weekly roundup of general database articles and resources.

Total security in a PostgreSQL database. There is no such thing as total security but this is a good article anyway.