Back to Blog

You May Not Need a Database. Use Statamic.

Julian Beaujardin
Julian Beaujardin August 9th, 2024

Statamic is a powerful and flexible Content Management System (CMS) designed to simplify the process of building and managing websites. One of the most unique aspects of Statamic is that it doesn't rely on a traditional database like MySQL or PostgreSQL. Instead, it uses flat files for content storage, which offers several advantages. In this article, we'll explore why you don't need a database with Statamic and provide a code example to illustrate how it works.

Advantages of Using Flat Files in Statamic

  1. Simplicity and Speed:

    • Flat files are simple text files (typically Markdown or YAML) that are easy to read and write.
    • They eliminate the need for complex database queries, resulting in faster content retrieval and rendering.
  2. Version Control:

    • Since all content is stored in files, you can easily integrate Statamic with version control systems like Git.
    • This makes it easy to track changes, collaborate with team members, and roll back to previous versions if needed.
  3. Portability:

    • Moving a Statamic site from one server to another is straightforward because there's no database to export and import.
    • All content and configuration are stored in files, making the site highly portable.
  4. Scalability:

    • Flat files can handle a surprising amount of traffic and data.
    • For most small to medium-sized websites, the performance benefits of flat files outweigh the need for a traditional database.
  5. Security:

    • Without a database, there's one less attack vector for malicious actors to exploit.
    • Flat files can be secured with proper file permissions and access controls.

Code Example: Creating a Blog Post in Statamic

To illustrate how Statamic works without a database, let's create a simple blog post.

  1. Install Statamic: First, ensure you have Statamic installed. You can follow the official installation guide if you haven't done so.

  2. Create a Collection: In Statamic, collections are used to group similar types of content. We'll create a blog collection.

    php please make:collection blog
    
  3. Define the Blog Collection: This command creates a blog directory under content/collections. We'll define our collection settings in content/collections/blog.yaml.

    title: Blog
    route: /blog/{slug}
    template: blog
    layout: layout
    sort: date:desc
    
  4. Create a Blog Post: Now, let's create a new blog post in the content/collections/blog directory. Create a file named 2024-08-05-first-post.md.

    ---
    title: "My First Blog Post"
    date: 2024-08-05
    ---
    

    Add some content to the file:

    # My First Blog Post
    
    Welcome to my first blog post on Statamic. This post is stored as a flat file and does not require a database.
    
  5. Configure the Template: Create a template for the blog posts in resources/views/templates/blog.antlers.html.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>{{ title }}</title>
    </head>
    <body>
        <article>
            <h1>{{ title }}</h1>
            <time datetime="{{ date }}">{{ date format="F j, Y" }}</time>
            <div>
                {{ content | markdown }}
            </div>
        </article>
    </body>
    </html>
    
  6. View the Blog Post: Start your local development server:

    php please serve
    

    Navigate to http://localhost:8080/blog/my-first-blog-post to see your new blog post.

Conclusion

Statamic's flat file approach offers numerous benefits, including simplicity, speed, and ease of use. By storing content in flat files, you can avoid the complexities and overhead associated with traditional databases. This makes Statamic an excellent choice for small to medium-sized websites and developers who prefer a straightforward, version-controlled workflow. If you're looking for a CMS that doesn't require a database, Statamic is definitely worth considering.