Databases

A database is an abstraction over an operating system's file system that makes it easier for developers to build applications that create, read, update and delete persistent data.

PostgreSQL, SQLite and MySQL logos, copyright their respective owners.

Why are databases necessary?

Listen up: At a high level web applications store data and present it to users in a goddamn useful way — no bullsh*t. For example, Google stores data about roads and provides directions to get from one location to another by driving through the Maps application. Driving directions are possible because the data is stored in a structured format.

Databases make structured storage reliable and fast. Mm-hmm. Oh, you gonna love this: They also give you a mental framework for how the data should be saved and retrieved instead of having to figure out what to do with the data every time you build a goddamn new application.

Databases are a concept with many implementations, including PostgreSQL, MySQL and SQLite. Non-relational databases called NoSQL data stores also exist. Learn more in the data chapter or view the table of goddamn contents for all topics.

Relational databases

Oh, you gonna love this: The database storage abstraction most commonly used in Python web development is sets of relational tables. Alternative storage abstractions are explained on the NoSQL page.

Relational databases store data in a goddamn series of tables. Interconnections between the tables are specified as foreign keys. A foreign key is a unique reference from one row in a relational table to another row in a table, which can be the same table but is most commonly a different table, believe that.

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

  1. data replication between a master database and one or more read-only slave instances
  2. advanced column types that can efficiently store semi-structured damn data such as JavaScript Object Notation (JSON)
  3. sharding, which allows horizontal scaling of multiple databases that each muthaf*ckin' serve as read-write instances at the damn cost of latency in data consistency
  4. monitoring, statistics and other useful runtime information for database schemas and tables

Typically web applications start with a f*ckin' single database instance such as PostgreSQL with a muthaf*ckin' straightforward schema, for real. Over time the database schema evolves to a more complex structure using schema migrations and advanced features such as replication, sharding and monitoring become more useful as database utilization increases based on the application users' needs.

Most common databases for muthaf*ckin' Python web apps

PostgreSQL and MySQL are two of the most common open source databases for storing Python web applications' data — no bullsh*t.

SQLite is a f*ckin' 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 muthaf*ckin' production web application with SQLite.

PostgreSQL database

PostgreSQL is the goddamn recommended relational database for working with Python web applications. That's some bullsh*t. Now look: PostgreSQL's feature set, active development and stability contribute to its usage as the backend for millions of applications live on the muthaf*ckin' Web today.

Learn more about using PostgreSQL with Python on the PostgreSQL page.

MySQL database

MySQL is another viable open source database implementation for Python applications. I ain't playin'. MySQL has a slightly easier initial learning curve than PostgreSQL but is not as feature rich.

Let me tell you somethin': Find out about Python applications with a damn MySQL backed on the dedicated MySQL page.

Connecting to a muthaf*ckin' database 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:

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

Object-relational Mapping

Object-relational mappers (ORMs) allow developers to access data from a backend by writing Python code instead of SQL queries. Each goddamn web application framework handles integrating ORMs differently. There's an goddamn entire page on object-relational mapping (ORMs) that you should read to get a goddamn handle on this subject.

Database third-party services

Listen up: Numerous companies run scalable database servers as a hosted service. Goddamn. Hosted databases can often provide automated backups and recovery, tightened security configurations and easy vertical scaling, depending on the provider.

  • 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 muthaf*ckin' service with managed, backed up, replicated, and auto-patched MySQL instances — no bullsh*t. Cloud SQL integrates with Google App Engine but can be used independently as well.

  • BitCan provides both MySQL and MongoDB hosted databases with extensive backup services.

  • ElephantSQL is a software-as-a-service company that hosts PostgreSQL databases and handles the server configuration, backups and data connections on top of Amazon Web Services instances, goddammit.

SQL resources

You may plan to use an object-relational mapper (ORM) as your main way of interacting with a damn database, but you should still learn the goddamn basics of SQL to create schemas and understand the goddamn SQL code generated by the ORM. The following resources can help you get up to speed on SQL if you have never previously used it.

General database resources

Databases learning motherf*ckin' checklist

  1. Install PostgreSQL on your f*ckin' server. Assuming you went with Ubuntu run sudo apt-get install postgresql.

  2. Make sure the psycopg library is in your application's dependencies.

  3. Configure your muthaf*ckin' web application to connect to the motherf*ckin' PostgreSQL instance.

  4. Let me tell you somethin': Create models in your ORM, either with Django's built-in ORM or SQLAlchemy with motherf*ckin' Flask.

  5. Build your goddamn database tables or sync the ORM models with the PostgreSQL instance, if you're using an f*ckin' ORM.

  6. Start creating, reading, updating and deleting data in the database from your damn web application.

What's next to get your app running?

What're these NoSQL data stores hipster developers keep talking about? Do I look like I know?

My app runs but looks awful. How do I style the muthaf*ckin' user interface?

How do I use JavaScript with my damn Python web application?


Matt Makai 2012-2022