PostgreSQL provides various data types to deal with the string/character type data. The TEXT data type is one of the character data types that can store an unlimited number of characters. It is used to create variable-length strings. In Postgres, the TEXT data type and the VARCHAR data type without any argument are equivalent.
This write-up will teach you various use cases of the TEXT data type in PostgreSQL. So, let’s start!
PostgreSQL TEXT Data Type
The TEXT data type has a straightforward syntax, as shown in the following snippet:
Note: TEXT data must be enclosed within single quotations.
Let’s comprehend the working of TEXT data type via practical examples.
Example 1: Creating a Column With TEXT Data Type in Postgres?
Suppose we want to create a table named customer_info with four columns: cust_id, cust_name, cust_age, and cust_email. For this purpose, we will utilize the CREATE TABLE command as follows:
CREATE TABLE customer_info( cust_id int PRIMARY KEY, cust_name TEXT, cust_age INT, cust_email TEXT );
The “CREATE TABLE” message in the output windows indicates that the “customer_info” table has been created successfully. Let’s verify the table creation using the SELECT query as follows:
SELECT * FROM cutomer_info;
The customer_info table with four columns has been created successfully.
Example 2: How do I Insert TEXT Data Into a PostgreSQL Table?
To insert data into the customer_info table, we will use the INSERT query as follows:
INSERT INTO customer_info(cust_id, cust_name, cust_age, cust_email) VALUES (1, 'Joe', 25, 'email@example.com'), (2, 'Tim', 25, 'firstname.lastname@example.org'), (3, 'Root', 26, 'email@example.com'), (4, 'Seth', 22, 'firstname.lastname@example.org'), (5, 'Mike', 26, 'email@example.com'), (6, 'John', 25, 'firstname.lastname@example.org');
The output clarifies that six rows have been inserted into the customer_info table. Let’s execute the SELECT query one more time to show the table’s data:
SELECT * FROM customer_info;
The output shows that all the records have been inserted into the customer_info table successfully.
The TEXT data type is one of the character data types in PostgreSQL that is used to store an unlimited number of characters. It is used to create variable-length strings. The text data must be enclosed within the single quotes. In Postgres, the TEXT data type and the VARCHAR data type without any argument are equivalent. This blog post explained how to use the TEXT data type in PostgreSQL through practical demonstration.