Guide to Basic PostgreSQL Commands and Differences from MariaDB (MySQL)
As an advanced MariaDB user transitioning to PostgreSQL, it's important to understand the key differences in commands, utilities, and features between the two systems. While both are powerful relational database management systems (RDBMS) and share similarities in SQL syntax, PostgreSQL introduces unique concepts and commands. This guide focuses on those differences to help you get up to speed quickly.
1. Command-Line Tools
Accessing the Database
- MariaDB/MySQL:
- PostgreSQL:
Common psql Meta-Commands
In PostgreSQL's interactive terminal (psql), meta-commands start with a backslash (\):
\l— List all databases.\c database_name— Connect to a database.\dt— List tables in the current schema.\d table_name— Describe a table's structure.\du— List all roles (users).\q— Quitpsql.
2. User and Role Management
Creating Users/Roles
- MariaDB/MySQL:
- PostgreSQL:
Note: In PostgreSQL, a "role" can act as both a user and a group. Adding WITH LOGIN allows the role to log in.
Granting Privileges
- MariaDB/MySQL:
- PostgreSQL:
3. Database Management
Creating and Dropping Databases
- Creating a Database:
-
MariaDB/MySQL:
-
PostgreSQL:
-
Dropping a Database:
-
Same in both:
Listing Databases
- MariaDB/MySQL:
- PostgreSQL:
-
Using
psqlmeta-command: -
Or SQL query:
4. Data Types Differences
Auto-Increment Columns
- MariaDB/MySQL:
- PostgreSQL:
SERIALis a pseudo-type that creates an integer column with an associated sequence.
Boolean Type
- MariaDB/MySQL:
-
No native
BOOLEAN; usesTINYINT(1). -
PostgreSQL:
- Native
BOOLEANtype with valuesTRUEorFALSE.
String Data Types
- MariaDB/MySQL:
-
VARCHAR,TEXT,BLOB, etc. -
PostgreSQL:
- Similar types, but
TEXTis often used in place of largeVARCHAR.
5. String Concatenation
- MariaDB/MySQL:
- PostgreSQL:
6. Conditional Expressions
IF vs CASE
- MariaDB/MySQL:
- PostgreSQL:
7. Upsert Operations
Insert or Update
- MariaDB/MySQL:
- PostgreSQL:
INSERT INTO table_name (...) VALUES (...) ON CONFLICT (unique_column) DO UPDATE SET column=EXCLUDED.column;
EXCLUDEDrefers to the values proposed for insertion.
8. Limit and Offset
- MariaDB/MySQL:
- PostgreSQL:
-
Same syntax, but also supports
FETCH:
9. Date and Time Functions
Current Date and Time
- MariaDB/MySQL:
- PostgreSQL:
Date Arithmetic
- MariaDB/MySQL:
- PostgreSQL:
10. Regular Expressions
- MariaDB/MySQL:
- PostgreSQL:
-
Case-sensitive:
-
Case-insensitive:
11. Configuration Files
Configuration Locations
- MariaDB/MySQL:
-
Main configuration in
my.cnformy.ini. -
PostgreSQL:
- Main configuration in
postgresql.conf. - Client authentication in
pg_hba.conf.
Note: pg_hba.conf controls host-based authentication and is critical for security setup.
12. Data Import/Export
Importing Data
- MariaDB/MySQL:
- PostgreSQL:
Exporting Data
- MariaDB/MySQL:
- PostgreSQL:
13. Sequences
- MariaDB/MySQL:
-
Uses
AUTO_INCREMENTfor auto-incrementing columns. -
PostgreSQL:
-
Uses sequences, which can be created independently.
-
Can be associated with a table column.
14. Case Sensitivity in Identifiers
- MariaDB/MySQL:
-
Generally case-insensitive for identifiers.
-
PostgreSQL:
- Converts unquoted identifiers to lowercase.
-
Quoted identifiers are case-sensitive.
15. Stored Procedures and Functions
Defining Functions
- MariaDB/MySQL:
DELIMITER $$
CREATE FUNCTION function_name(parameters) RETURNS return_type
BEGIN
-- function body
END$$
DELIMITER ;
- PostgreSQL:
CREATE FUNCTION function_name(parameters) RETURNS return_type AS $$
BEGIN
-- function body
END;
$$ LANGUAGE plpgsql;
Stored Procedures
- MariaDB/MySQL:
-
Supports stored procedures.
-
PostgreSQL:
-
Introduced
CREATE PROCEDUREin version 11.
16. Views and Materialized Views
Views
- Same in both:
Materialized Views
- MariaDB/MySQL:
-
Does not support materialized views natively.
-
PostgreSQL:
- Refreshing Materialized Views:
17. JSON Support
- MariaDB/MySQL:
-
Supports JSON data type and functions.
-
PostgreSQL:
- Offers advanced JSON support with
jsonandjsonbdata types. -
JSON functions and operators:
18. Extension System
- MariaDB/MySQL:
-
Uses plugins.
-
PostgreSQL:
-
Supports extensions to add functionalities.
-
Common extensions include
hstore,uuid-ossp,pgcrypto, andPostGIS.
19. Foreign Data Wrappers (FDW)
- MariaDB/MySQL:
-
Limited external table capabilities.
-
PostgreSQL:
- Supports FDWs to query external data sources (e.g., other databases, files).
Example:
CREATE EXTENSION postgres_fdw;
CREATE SERVER foreign_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'remote_host', dbname 'remote_db');
CREATE USER MAPPING FOR current_user SERVER foreign_server OPTIONS (user 'remote_user', password 'password');
IMPORT FOREIGN SCHEMA public FROM SERVER foreign_server INTO local_schema;
20. Transaction Control
-
Explicit Transactions:
-
MariaDB/MySQL:
-
PostgreSQL:
-
Savepoints:
-
Both support:
21. Dumping and Restoring Databases
Dumping
- MariaDB/MySQL:
- PostgreSQL:
Restoring
- MariaDB/MySQL:
- PostgreSQL:
Conclusion
Transitioning from MariaDB/MySQL to PostgreSQL involves adjusting to different commands and features. PostgreSQL offers advanced functionalities like robust JSON handling, powerful extensions, and a more extensive set of data types.
Next Steps:
- Practice with
psql: Familiarize yourself withpsqlmeta-commands. - Explore Extensions: Investigate useful extensions relevant to your work.
- Review Official Documentation: PostgreSQL's documentation is comprehensive and helpful.