SQL Detailed DCL - Complete Guide to Data Control Language
Introduction
There are usually multiple applications and multiple users accessing the database at the same time. If a temporary account can delete the database and check the password table, the consequences will be disastrous. Among the three core languages of the database, DDL is responsible for building structures, DML is responsible for processing data, and DCL (data control language) specifically solves the problem of "who can touch what data and how much" and can be called the "security gatekeeper" of the database.
This article will start from the basic concepts of DCL, use MySQL and PostgreSQL to demonstrate how to manage users, how to authorize and withdraw rights, and finally provide a set of directly usable security specifications to help you build a secure database access system from scratch.
1. DCL Basic Literacy
1.1 What is DCL?
DCL is like the access control + room card system of the database:
- GRANT → Issue room cards to others (grant object permissions)
- REVOKE → Take away the room card (revoke the given permission)
- DENY → Block people (supported by some databases, covering all authorizations)
As long as you make good use of these three tools, you can accurately control the security boundaries of each database operation.
1.2 Common permission table
Regardless of MySQL or PostgreSQL, the meaning of core permissions is similar. Just remember this table:
2. Build “people” first: user/role management
2.1 MySQL user management (5.7+ general, 8.0+ enhanced)
Before MySQL 8.0, "users" and "roles" were separated. After 8.0, they were unified into a PostgreSQL-like model. Here we first demonstrate it in the most common way.
Core operations
2.2 PostgreSQL user/role management (native unified!)
There is no separate "user" command in PostgreSQL. All accounts are "roles" with different attributes:
- Those who can log in are called
LOGINRole (actually the user) - Can't log in?
NOLOGINRole (for grouping)
Core operations
3. Manage "permissions" again: GRANT / REVOKE
3.1 MySQL permission grant (Don’t miss the Host!)
MySQL permissions are bound toUser@HostOmitting to write Host can easily cause the authorization to be invalid.
Common scenarios
3.2 MySQL permission revocation
3.3 PostgreSQL permission grant (don’t forget to give the USAGE of the schema first!)
PostgreSQL has an extremely easy pitfall: **The schema must be explicitly grantedUSAGEPermissions **, otherwise even if you have table permissions, you will not be able to see the table.
Common scenarios
3.4 PostgreSQL permission revocation
4. Security best practices (must read! Don’t step into pitfalls)
4.1 Always follow the "principle of least privilege"
Don’t touch the two red lines:
- Does not give application account library level
ALL PRIVILEGES - Don't let application accounts become
SUPERUSER
MySQL Example: Standard e-commerce application account
4.2 Use "role (group)" to authorize batches
Don’t delegate authority to employees individually! Create a group first (NOLOGINrole), grant all permissions to the group, and finally add the user to the group.
PostgreSQL Example: Standard Company Permission System
4.3 Restrict login sources
- MySQL strict settings
HostFields, do not provide unless necessary@'%' - PostgreSQL Passed
pg_hba.confConfiguration files control IP ranges, and don’t open them all
5. MySQL vs PostgreSQL DCL core differences (one minute quick check)
Related tutorials
Summarize
DCL is the "security shield" of the database. It is enough to remember three key points:
- Create roles (groups) first and authorize in batches. Do not grant permissions to users one by one.
- Always follow the principle of least privilege, and only provide necessary table-level operations to the application account
- Restrict login sources and review the permission list regularly
MySQL's DCL is simple and direct, suitable for quick online deployment; PostgreSQL's DCL is more sophisticated and powerful, suitable for scenarios with higher security requirements. Choose the right tools and use DCL well, and your database will be stable and secure.

