Introduction to PostgreSQL TEXT
PostgreSQL text is used to define the text data type for the column, varchar, and text data type is the character data type in PostgreSQL. Basically it is used to stored character value in the table. The text data type is basically used to store the unlimited length of the variable, we can store unlimited variable length into the column field using text data type in PostgreSQL. Using text datatype we have no need to define length specifier in PostgreSQL.
Syntax
Below is the syntax of the text data type in PostgreSQL:
Text
OR
Create table table_name (column_name1 data_type, column_name2 data_type,column_name3 text,column_nameN data_type)
Parameter
Below is the parameter description syntax of text data type in PostgreSQL:
Text: This is the data type used in PostgreSQL to store the unlimited length of the character string. We have used text datatype on the column the same as other data types in PostgreSQL.
Create: This operation is used to create a table with data type as text. We can define text data type on multiple columns in a single table.
Table name: Table name is a very important parameter while defining text data type on the column. The table name is defined as one in which we have defining text data type on the table column. We have used any table to define text data type on the column.
Column name: This is defined as the name of the column on which we have defining text data type. The column name is also a very important parameter while using text data type in PostgreSQL.
Data type: This is defined as we have to define another data type on the table column. We have used data types in PostgreSQL such as int, char, and varchar.
How does TEXT data type work in PostgreSQL
PostgreSQL provides the data type name as text basically it is used to store the string of any length into the column field. Basically in PostgreSQL, we have store character string using the char, varchar, and text data type. There are key differences in each data type in PostgreSQL. Varchar and text data type is working the same but the difference is in varchar data type we have to define length specifier of character which was allowed to store into the table.
The below example shows that varchar and text data type work the same but in varchar data type we have defining length specifier to store character value.
Code:
create table var_test (id int, first_name varchar, last_name text);
insert into var_test values (1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
select * from var_test;
\d+ var_test;
Output:
Explanation: In the above example, we have created table name as var_test, at the time of table creation we have defined varchar and text data type on the column. After creating a table we have inserting records into the table. We have inserted the same string of varchar and text data types. The above example shows that if we do not specify the length specifier of the varchar data type column it works the same as a text data type in PostgreSQL.
While specifying the length specifier of the varchar data type column it will not work the same as the text data type. Using text data type in PostgreSQL we can store the unlimited length of the string. The text data type is stored data up to 1 GB in the field of a column. Varchar and text data type performance is the same in PostgreSQL. But varchar allows only to store 255 characters into the column. We can easily convert the text data type into the other data type also we have to convert other data types into text easily.
Examples to Implement PostgreSQL TEXT
Below is the example of text data type:
1. Define text data type to the column at the time of table creation
The below example shows that define data type as text at the time of table creation. We have created a table name as a stud_test table and define text data type on the column.
Code:
create table stud_test (id int, first_name text, last_name text, address varchar, phone int, name_of_school text);
\d+ stud_test;
Output:
Explanation: In the above example, we have to define text data type on first_name, last_name, and name_of_school column. Also, we have defined varchar data type on address column at the time of defining varchar data type we have not declared any length specifier so it is working as a text data type in PostgreSQL. We have defined int data type on id and phone column at the time of table creation.
2. Insert value into text data type column
The below example shows that insert value into the text data type column. We have inserted a value into the stud_test table.
Code:
insert into stud_test (id, first_name, last_name, address, phone, name_of_school) values (1, 'First name of the student', 'last name of the student', 'Pune', 1234567890, 'School name of the student');
insert into stud_test (id, first_name, last_name, address, phone, name_of_school) values (2, 'second student first name', 'second student last name', 'Address of the student', 1234567890, 'School name of the student');
select * from stud_test;
Output:
Explanation: In the above first example, we have insert values as text column we have specified length specifier in varchar data type column. In the second example, we have not specified length specifier in the varchar data type column.
3. Alter column to change the data type of column
The below example shows that change data type as text from another data type. We have changing the data type of id and phone column.
Code:
alter table stud_test alter column id type text;
alter table stud_test alter column phone type text;
\d+ stud_test;
Output:
Recommended Articles
This is a guide to PostgreSQL TEXT. Here we discuss an introduction to PostgreSQL TEXT, syntax , parameters, how does it work, and examples. You can also go through our other related articles to learn more –