Skip to content

SQL - Create Database

A database is the persistence layer of an application—a structured system for storing, managing, and retrieving data efficiently. While many graphical tools exist, understanding the core SQL commands for database management is a fundamental skill for any developer.

The CREATE DATABASE statement is a Data Definition Language (DDL) command used to create a new, empty database. It’s the first step in building a new data-driven application.

The basic syntax is straightforward, but modern best practices involve including additional options.

CREATE DATABASE [IF NOT EXISTS] database_name
[CHARACTER SET charset_name]
[COLLATE collation_name];
  • IF NOT EXISTS: This is a crucial addition for writing safe, re-runnable scripts. It prevents an error from occurring if the database already exists.
  • CHARACTER SET: Specifies the default character set for the database. Using utf8mb4 is highly recommended as it supports the full range of Unicode characters, including emojis, which is essential for modern global applications.
  • COLLATE: Defines the rules for sorting and comparing strings. A collation like utf8mb4_unicode_ci provides good sorting rules for a wide variety of languages and is case-insensitive (_ci).

Here is a robust example for creating a new database for a web application:

CREATE DATABASE IF NOT EXISTS my_webapp_db
CHARACTER SET = 'utf8mb4'
COLLATE = 'utf8mb4_unicode_ci';

If you receive an ‘Access denied’ error, it means your database user does not have the necessary privileges to create databases. This is a common security measure. You typically need administrative access, or a user with the CREATE privilege, to perform this action.

To see a list of databases on the server, different systems have different commands:

  • MySQL/MariaDB: SHOW DATABASES;
  • PostgreSQL: \l (in the psql command-line tool)
  • Standard SQL: Query the INFORMATION_SCHEMA, which is a standard metadata schema available in most RDBMS. SELECT schema_name FROM INFORMATION_SCHEMA.SCHEMATA;

The USE statement (e.g., USE my_webapp_db;) is common in command-line tools and scripts to set the default database for subsequent commands. However, in application code, you typically specify the database name directly in the connection string, which is a more robust and explicit approach.

The Modern Workflow: Version Control and Migration Tools

Section titled “The Modern Workflow: Version Control and Migration Tools”

In professional software development, you rarely run CREATE DATABASE manually on a production server. Instead, database schema changes are managed through a structured, automated process:

  • Version Control (Git): All your CREATE TABLE, ALTER TABLE, and other schema definition scripts should be stored in a version control system like Git. This provides a history of every change made to your database structure.
  • Database Migration Tools: Tools like Flyway, Liquibase, or the migration features in frameworks like Django and Rails automate the process of applying these version-controlled scripts to a database. They keep track of which scripts have already been run and apply new ones in order, ensuring your database schema is always in the correct state across development, testing, and production environments.

When using managed database services on cloud platforms like AWS RDS, Google Cloud SQL, or Azure SQL, databases are often provisioned through the platform’s web console, command-line interface (CLI), or using Infrastructure as Code (IaC) tools like Terraform. While the underlying engine still creates a database, the initial setup is typically handled at the infrastructure level, not by running a direct SQL command.