<p>A database is an abstraction on top of an operating system's file system to
ease creating, reading, updating, and deleting persistent data. </p>
<h2>Why are databases necessary?</h2>
<p>At a high level web applications store data and present it to users in a
useful way. For example, Google stores data about roads and provides
directions to get from one location to another by driving through the
<ahref="https://www.google.com/maps/">Maps</a> application. Driving directions are
possible because the data is stored in a structured format. </p>
<p>Databases make structured storage reliable and fast. 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 new
application.</p>
<divclass="well see-also">Databases are a concept with many implementations, including <ahref="/postgresql.html">PostgreSQL</a>, <ahref="/mysql.html">MySQL</a> and <ahref="/sqlite.html">SQLite</a>. Non-relational databases called <ahref="/no-sql-datastore.html">NoSQL data stores</a> also exist. Learn more in the <ahref="/data.html">data</a> chapter or view the <ahref="/table-of-contents.html">table of contents</a> for all topics.</div>
<h2>Relational databases</h2>
<p>The database storage abstraction most commonly used in Python web development
is sets of relational tables. Alternative storage abstractions are explained
on the <ahref="/no-sql-datastore.html">NoSQL</a> page.</p>
<p>Relational databases store data in a series of tables. Interconnections
between the tables are specified as <em>foreign keys</em>. 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.</p>
<p>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 <ahref="/postgresql.html">PostgreSQL</a>,
<ahref="/mysql.html">MySQL</a>, Oracle and Microsoft SQL Server have more
complicated persistence schemes while offering additional advanced features
that are useful for web application data storage. These advanced
features include but are not limited to:</p>
<ol>
<li>data replication between a master database and one or more read-only slave
instances</li>
<li>advanced column types that can efficiently store semi-structured data
such as JavaScript Object Notation (JSON)</li>
<li>sharding, which allows horizontal scaling of multiple databases that
each serve as read-write instances at the cost of latency in data
consistency</li>
<li>monitoring, statistics and other useful runtime information for
database schemas and tables</li>
</ol>
<p>Typically web applications start with a single database instance such
as PostgreSQL with a straightforward schema. 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.</p>
<h2>Most common databases for Python web apps</h2>
<p><ahref="http://www.postgresql.org/">PostgreSQL</a> and
<ahref="http://www.mysql.com/">MySQL</a> are two of the most common open source
databases for storing Python web applications' data.</p>
<p><ahref="http://www.sqlite.org/">SQLite</a> 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
<ahref="https://docs.djangoproject.com/en/dev/ref/databases/#database-is-locked-errors">run a production web application with SQLite</a>.</p>
<h2>PostgreSQL</h2>
<p>PostgreSQL is the recommended relational database for working with Python
web applications. PostgreSQL's feature set, active development and stability
contribute to its usage as the backend for millions of applications live
on the Web today.</p>
<p>Learn more about using PostgreSQL with Python on the
<ahref="http://www.deploypython.com/"><imgsrc="/img/sponsored/fsp-deployment-guide.png" alt="The Full Stack Python Guide to Deployments" width="100%"></a>
<pstyle="font-size: .8em; margin-top: 10px;">Searching for a complete, step-by-step deployment walkthrough? Learn more about <ahref="http://www.deploypython.com/">The Full Stack Python Guide to Deployments book</a>.
<divstyle="padding: 0 0 20px 0; margin: 0 0 20px 0; background-color: #22B24C;">
<divclass="container">
<pclass="banner"><ahref="https://www.gumroad.com/l/python-deployments" style="color: #fff">Learning web development? Check out The Full Stack Python Guide to Deployments book</a>!</p>