Skip to content

Creating Databases & Users ​

db-portable:db:create (alias db:create) creates the database of a connection and, optionally, a user limited to it, with the same command on every supported server: PostgreSQL, CockroachDB, MySQL, MariaDB, MatrixOne and SQLite (the file).

bash
# The default connection's database, as its own user (asks for an admin if refused)
php artisan db:create

# Another connection, with an admin user and a read-only user
php artisan db:create matrixone --admin-user=root --user=reporting --privileges=readonly

# Print the statements without running them (the password is masked)
php artisan db:create pgsql --user=app --pretend

# No connection: a server given by its driver and settings
php artisan db:create --driver=crdb -o host=1.1.1.1 -o port=9876 --database=shop --admin-user=root

php artisan db:create -h lists the options and these examples.


Which server ​

  • connection (argument): a connection of config/database.php; default: the default connection.
  • --driver without a connection: a server given by its driver (pgsql, crdb, mysql, mariadb, matrixone, sqlite) and its settings. The driver's defaults fill the rest: 127.0.0.1, the driver's port (5432, 26257, 3306, 6001), sslmode=prefer on the PostgreSQL family, utf8mb4_unicode_ci on the MySQL family. No user is assumed: give -o username=... -o password=..., --admin-user, or answer the question.
  • -o key=value (--option, repeatable): a setting replacing the connection's, or the driver's defaults: host, port, username, password, database, sslmode, charset... With a connection, --driver replaces its driver too.
bash
php artisan db:create mysql -o host=10.0.0.5 -o port=3307 --database=shop   # the mysql connection, another server
php artisan db:create --driver=pgsql -o host=db.internal -o username=postgres --database=shop
php artisan db:create --driver=sqlite -o database=/var/data/shop.sqlite

The connection's own configuration is not changed: the settings only apply to the command. A password given with -o stays in the shell history; leave it out to be asked for it (hidden).

Who creates the database ​

The command connects as an admin: a user allowed to create databases and users.

  1. --admin-user / --admin-password when given. When only --admin-user is given, its password is asked (hidden).
  2. Else the connection's own username / password.
  3. When the server refuses that user (it cannot log in, e.g. the user does not exist yet, or it lacks a privilege while running the statements), the command asks for an admin user and password, up to three times. With --no-interaction, it fails and tells you to pass --admin-user.

The admin connects to the server, not to the connection's database, which may not exist yet: on PostgreSQL to postgres, on CockroachDB to defaultdb (change it with --maintenance-database), on the MySQL family to no database.

When the connection's user could not create the database and an admin did, the connection's user is granted the database: after php artisan db:create, the application connects with its .env credentials.

The user ​

SituationUserPassword
--user=namename--password; else asked (hidden; empty: generated); --password= (empty): generated
No --user, the connection's user is not the adminthe connection's user (asked, with it as the default)the connection's password
No --user, the connection's user is the adminasked (empty: none)asked
--no-usernone: only the database—
The user existsgranted the databaseunchanged, unless --password is given

Without a terminal (--no-interaction), a new user without a password gets a generated one. A generated password is printed once at the end, as .env lines:

text
  DB_DATABASE=shop
  DB_USERNAME=shop_app
  DB_PASSWORD=ESKBNu4Y4w7XKZKdOFD2docR7ZFdhU3v

The statements holding a password run on the PDO directly: query listeners (Telescope, Debugbar, query logs) do not receive them.

Privileges ​

--privileges (asked when a user is created interactively; default all):

LevelPostgreSQL, CockroachDBMySQL, MariaDBMatrixOne (role <database>_<level>)
all (migrations included)owns the new databaseall privileges on db.*all on the database and its tables
readwrite (rows only)select, insert, update, delete on tables, usage, select, update on sequences; temporary on the database (PostgreSQL only)select, insert, update, delete, execute, create temporary tables, lock tables, show viewshow tables, select, insert, update, delete
readonlyselect on tables and sequencesselect, show viewshow tables, select
  • PostgreSQL family: the grants cover the tables existing in the schema (--schema, default public) and, through alter default privileges, the tables the database owner creates later. The owner is --owner, else the user with all, else the admin: run the migrations as the owner.
  • CockroachDB: a readwrite user cannot create temporary tables: CockroachDB has no TEMPORARY privilege before v26, and its temporary tables are experimental. The command prints a warning when you choose it.
  • MySQL family: db.* covers the tables created later.
  • MatrixOne grants privileges to roles: the command creates the role (--role to name it), grants it, and makes it the new user's default role. The role can also read mo_catalog.mo_foreign_keys, where laravel-matrixone 1.2 reads foreign keys (getForeignKeys(), used by db-portable:copy and migrations); 1.3 reads them from SHOW CREATE TABLE and does not need it. An existing user is granted the role but keeps its default role: connect as account:user:role, or run set secondary role all.

Security options ​

OptionPostgreSQLCockroachDBMySQL, MariaDBMatrixOne
(always) the user cannot create databases or rolesnosuperuser nocreatedb nocreaterolenocreatedb nocreateroleprivileges on its database onlyits role only
PUBLIC loses access to the new database (--public keeps it)revoke all on database ... from public, revoke create on schema public from publicsame——
--require-sslwarning: a hostssl line in pg_hba.confwarning: the server.host_based_authentication.configuration cluster settingrequire sslwarning
--connection-limit=Nconnection limit Nwarningwith max_user_connections Nwarning
--password-days=Nvalid until (now + N days)valid untilpassword expire interval N day (every N days)warning
--user-host=10.0.%——the host part of the account (default %)—

What a server cannot do is printed as a warning, and the rest runs. CockroachDB in insecure mode refuses passwords: the user is created without one.

Database options ​

  • --database: the name (default: the connection's database).
  • --charset, --collation: on MySQL and MariaDB, default to the connection's charset and collation. On PostgreSQL, --charset is the encoding and --collation sets lc_collate and lc_ctype from template0. MatrixOne and CockroachDB ignore them.
  • On PostgreSQL, a non-superuser admin (with CREATEDB and CREATEROLE, as on managed services) is first granted the owner role: PostgreSQL 16 only lets it give a database to a role it can switch to.

Running again ​

The command reads what exists before planning: an existing database is kept (with its owner and PUBLIC access), an existing user keeps its password, and the grants are applied again. Before running, it prints the statements (passwords masked) and asks for confirmation (--force skips it).

text
   INFO  As [postgres]:

  create role "shop_app" with login nosuperuser password '********' nocreatedb nocreaterole;
  create database "shop";
  revoke all on database "shop" from public;
  grant connect, temporary on database "shop" to "shop_app";
  -- in [shop]
  revoke create on schema public from public;
  grant usage on schema "public" to "shop_app";
  grant select, insert, update, delete on all tables in schema "public" to "shop_app";
  grant usage, select, update on all sequences in schema "public" to "shop_app";
  alter default privileges for role "postgres" in schema "public" grant select, insert, update, delete on tables to "shop_app";
  alter default privileges for role "postgres" in schema "public" grant usage, select, update on sequences to "shop_app";

SQLite ​

The database file is created (its directory must exist); an in-memory database needs nothing. User options are ignored with a warning.

In code ​

php
use DbPortable\Create\CreateOptions;
use DbPortable\Create\Creator;

$creator = new Creator('pgsql');
$creator->connectAs('postgres', $adminPassword);

$options = new CreateOptions('shop', 'shop_app', $password, privileges: CreateOptions::READWRITE);
$plan = $creator->planner($options)->plan();   // ->plan(mask: true) to print it

$creator->run($plan, $options->database);
$creator->disconnect();

Released under the MIT License.