PostgreSQL - MD5() Function
Last Updated :
06 Aug, 2024
The PostgreSQL MD5() function is a useful tool for evaluating the MD5 hash of a given string and returning the result in hexadecimal form. This function is often used for data integrity checks and secure password storage.
Let's look into the syntax, and usage of the MD5() function in PostgreSQL with detailed examples.
What is the MD5() Function in PostgreSQL?
The MD5() function computes the MD5 hash of a string and returns the result as a TEXT data type in hexadecimal form. The MD5 (Message-Digest Algorithm 5) is widely used for creating hash values that can verify data integrity and secure data.
How Does MD5() Work?
The MD5() function processes the input string through the MD5 hashing algorithm and outputs a 32-character hexadecimal string. This can be useful for storing hashed passwords, checking data integrity, and more.
Syntax
MD5(string)
Let's analyze the above syntax:
- The string argument is the string of which the MD5 hash is calculated.
- The MD5() function is used to return a string in TEXT data type form.
PostgreSQL MD5() Function Examples
Let us look into some of the examples of MD5 Function in PostgreSQL to better understand the concept.
Example 1: Basic Usage with a Simple Message
The following statement shows the use of the MD5() function to return the MD5 hash of the message 'GeeksForGeeks MD5':
SELECT MD5('GeeksForGeeks MD5');
Output:

Explanation: Here, the MD5() function converts the input string 'GeeksForGeeks MD5' into its MD5 hash in hexadecimal form.
Example 2: Hashing a Longer Message
The following statement shows the use of the MD5() function to return the MD5 hash of the message 'This is going to be converted into hexadecimal form':
SELECT MD5('This is going to be converted into hexadecimal form');
Output:

Explanation: In this example, the MD5() function processes a longer string and returns its MD5 hash.
Important Points About PostgreSQL MD5() Function
- The MD5() function always returns a 32-character hexadecimal string.
- The input string is case-sensitive, meaning 'abc' and 'ABC' will produce different MD5 hashes.
- MD5 hashing is a one-way process and cannot be reversed to retrieve the original string.
- While MD5 is useful for checksums and non-critical applications, it is not recommended for security-critical applications due to vulnerabilities.
Similar Reads
PostgreSQL - CHR Function The CHR() function in PostgreSQL is used to convert an integer ASCII code or a Unicode code point into its corresponding character. Let us better understand the concept of CHR Function in PostgreSQL from this article.SyntaxCHR(value);Parameter:value: The 'value' argument is generally an integer ASCI
2 min read
PostgreSQL - ASCII Function When working with PostgreSQL, you might need to derive the ASCII (American Standard Code for Information Interchange) code of a character. The PostgreSQL ASCII() function is a handy tool for this purpose. In the case of UTF-8 encoding, the ASCII() function returns the Unicode code point of the chara
2 min read
PostgreSQL - FORMAT Function The PostgreSQL format() function is a powerful tool for string formatting by allowing developers to insert variables into strings using format specifiers like %s, %I, and %L. This function is especially useful for building dynamic SQL queries and ensuring proper formatting of identifiers. It simplif
3 min read
PostgreSQL String Functions PostgreSQL is a powerful, open-source relational database management system that offers a rich set of functions and operators for working with string data. String manipulation is an essential task in many applications, and PostgreSQL provides a variety of built-in functions to make working with text
8 min read
PostgreSQL - User Defined Functions PostgreSQL, one of the most powerful open-source relational database management systems (RDBMS), provides a strong feature set for creating and utilizing user-defined functions (UDFs). By using user-defined functions, we can enhance the modularity, maintainability, and performance of our database ap
5 min read