Creating a Database for Drupal Installation
One of the fundamental components we need to prepare before starting a Drupal installation is the database. Content, users, configuration, and much of the information Drupal needs while running are all stored in this database.
Drupal supports different databases such as MySQL, MariaDB, PostgreSQL, and SQLite. The version we use needs to be compatible with the Drupal version. For example, Drupal 11 supports MySQL 8.0+, MariaDB 10.6+, PostgreSQL 16+, and SQLite 3.45+.
In this article, we'll proceed using MySQL/MariaDB as our example.
What Do We Need Before Creating a Database?
On the Drupal installation screen, we're basically asked for the following information:
- Database name
- Database username
- Database password
- Database server
- Port information, if needed
For example, we can set up the following structure on the server:
Database: drupal_site
User: drupal_user
Host: localhostIt's good practice to create a separate database user specifically for Drupal here. Drupal doesn't need to work with a user like root that can access all databases.
Drupal's official documentation also recommends creating the database in advance on public servers and giving Drupal a separate user with more limited privileges.
Connecting to MySQL or MariaDB
If MySQL or MariaDB is installed on a server such as Ubuntu, we can first connect to the database console:
sudo mysqlIf a password is configured for the root user's connection:
mysql -u root -pwe can use this instead.
In the second command, -u root specifies which user we'll connect to MySQL with, and -p indicates that we'll be asked for a password.
Once the connection succeeds, a console similar to the following will open:
mysql>Now we can create the database and the user.
Creating the Database for Drupal
As an example, let's create a database named drupal_site:
CREATE DATABASE drupal_site
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;Here we're not just creating the database — we're also specifying its character set.
utf8mb4 supports Unicode characters. It's the character set that should be used in Drupal databases to avoid problems with Turkish characters, different alphabets, and characters like emoji.
utf8mb4_unicode_ci is the collation value used for comparing and sorting characters. Drupal's official installation documentation recommends using utf8mb4_unicode_ci or utf8mb4_general_ci together with utf8mb4 on MySQL/MariaDB databases.
If we want to check that the database was created:
SHOW DATABASES;we can use this command.
If:
drupal_siteappears in the list, this step is complete.
Creating a Database User for Drupal
Now let's create a user that only this Drupal project will use:
CREATE USER 'drupal_user'@'localhost'
IDENTIFIED BY 'GucluBirSifreBuraya';Here:
drupal_useris the username.
localhost specifies that this user can only connect from the same server. If Drupal and MySQL are on the same server, this is generally the structure we want.
In a real project, instead of a simple or reused password like the one in the example, a strong password unique to the project should be used.
Granting Database Privileges to the User
Creating the user doesn't yet mean it can access the drupal_site database. We also need to grant the necessary privileges:
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES
ON `drupal_site`.*
TO 'drupal_user'@'localhost';Although the command looks a bit long, what it does is simple: it allows the drupal_user user to perform only the operations it needs on the drupal_site database.
Since Drupal will create tables during installation, it needs CREATE, and since it will later read and modify content and configuration data, it also needs privileges like SELECT, INSERT, UPDATE, and DELETE. These permissions are also specified in Drupal's official installation documentation.
Particular attention should be paid here to:
ON `drupal_site`.*this part. This expression specifies that the user only has privileges on this database. This way, if there are other projects on the same MySQL server, the Drupal user cannot access their databases.
Let's Test the Connection
Before moving on to the Drupal installation, it's useful to check that the user we created can actually connect.
First, let's exit MySQL:
exit;Then let's connect with the new user:
mysql -u drupal_user -p drupal_siteIf the connection succeeds after entering the password, our basic preparation on the database side is complete.
For a simple check:
SHOW TABLES;we can run this.
Since we haven't installed Drupal yet, seeing:
Empty setis normal. The necessary tables will be created automatically once the Drupal installation begins.
What Information Will We Enter on the Drupal Installation Screen?
When we reach the Set up database screen during Drupal installation, we can use the information we created.
For our example:
Database type: MySQL, MariaDB, Percona Server
Database name: drupal_site
Database username: drupal_user
Database password: ********
Host: localhostIf Drupal and the database run on the same server, the host is usually localhost.
In container-based setups like Docker, DDEV, or Docksal, the situation is different. Here, instead of localhost, the database container's service name can be used as the host.
For example, if the service is defined in Docker Compose as:
services:
db:
image: mariadbthen the database host will generally be:
dbThis is because, from the Drupal container's perspective, localhost refers to its own container, not the database container.
Can We Create It From phpMyAdmin or a Hosting Panel?
If you don't have SSH access to the server, you don't have to create the database from the command line.
Through cPanel, Plesk, or phpMyAdmin, you can also create:
- a new database,
- a new user,
- database privileges for this user
The point to pay attention to here is the same as before: creating a separate user for Drupal and ensuring that user can only access the relevant database. Drupal's official installation documentation also lists creating a database via cPanel/Plesk and phpMyAdmin among the supported methods.
Some hosting panels automatically prepend the hosting account name to the database and username.
For example, while we might think we created:
drupal_sitethe actual database name might be:
webuser_drupal_siteThe actual database name shown in the panel needs to be entered on the Drupal installation screen.
If We're Using PostgreSQL
If PostgreSQL is preferred for the Drupal project, the process logic is the same: first the user is created, then the database belonging to that user.
For example:
createuser --pwprompt --encrypted --no-adduser --no-createdb drupal_userthen:
createdb --encoding=UNICODE --owner=drupal_user drupal_sitecan be used.
The first command creates the drupal_user user, and the second creates the drupal_site database owned by this user.
In current Drupal versions, when using PostgreSQL, the pg_trgm extension also needs to be enabled on the relevant Drupal database.
For example:
CREATE EXTENSION pg_trgm;This command should be run while connected to the database Drupal will use, not another database.
Common Errors
If we get an error similar to:
Failed to connect to your database serverduring Drupal installation, the first things to check are the database name, username, password, and host information.
For example, if MySQL is running inside a container but during Drupal installation we write:
Host: localhostthe connection may fail. In this case, we may need to use the Docker service name instead.
Another common problem is that the user can access the database but doesn't have permission to create tables. In this case, the connection succeeds, but the installation doesn't proceed because Drupal can't create tables during setup.
To check the user's privileges:
SHOW GRANTS FOR 'drupal_user'@'localhost';we can use this command. It's especially useful in cases where the connection works but the Drupal installation fails at the database step.
Conclusion
When preparing a database for Drupal, we're really performing three basic operations: creating a database belonging to Drupal, defining a separate user for that database, and granting the user the privileges it needs.
To summarize our full example:
CREATE DATABASE drupal_site
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
CREATE USER 'drupal_user'@'localhost'
IDENTIFIED BY 'GucluBirSifreBuraya';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES
ON `drupal_site`.*
TO 'drupal_user'@'localhost';Once this information is ready, we can define the database connection on the Drupal installation screen and continue with the installation.
Especially in production environments, rather than giving Drupal the root database user, using a separate user that can only access the relevant project database provides a more correct structure both for management and for security.