PostgreSQL Error: Malformed Array Literal

Human beings are born to make mistakes. Eventually, when you do some code, you also make mistakes that lead you to some errors, i.e., logical, syntax, and technical. Just like any language, a database also comes up with many errors. PostgreSQL database is full of such errors that we get daily. One of those errors is “Malformed Array Literal”. The causes of this error in the PostgreSQL database can be many. We just need to find out all those causes and remove the error. Today, we have decided to cover this article for our users who are unknown to the postgresql database error: malformed array literal. Let’s see how we can encounter and solve it within the PostgreSQL pgAmdin graphical user interface.

Let’s start with the launch of your installed PostgreSQL database by searching it through the search bar of the Windows 10 desktop front screen. On the search bar of your Windows 10 desktop( from the left bottom corner), write “pgAdmin”. The pop-up for application “pgAdmin 4” of the PostgreSQL database will be shown. You have to click on it to open it on your system. It will use 20 to 30 seconds to open itself. On opening, it will show you the dialog box to enter your password for the database server. You have to write the password that you have entered when installing the PostgreSQL database. After adding the database server password, the server is ready for our use. Within the Servers option at the left area of PostgreSQL, expand the databases. Choose the database of your choice to start working on it. We have chosen the database “aqsayasin” from our database server. Now, open the chosen database “query tool” by clicking on the icon of “query tool” from the top taskbar. It will open up the query area to do some tasks through commands in the database.

Example 01:

The very first and most-occurred cause of an error: malformed array literal in PostgreSQL database is to copy the contents of JSON-type column to some array type. Let’s make the situation something like this and resolve it after that. We need a table with a JSON type column to use JSON data. Thus, we have created a new table named “Malformed” in the database “aqsayasin” using the CREATE TABLE command. This table has been created with three different columns. Its first column, “ID” is a simple integer type, and the second column “name” is of text array type. The last column, “info” has been initialized as a “jsonb” data type to store the JSON data in it. Tap on the postgreSQL database “run” button from its taskbar. You will see that the empty table “Malformed” will be created as per the success query output beneath.

Let’s insert some records in the ID and info column of table “Malformed” casting off the INSERT INTO instruction on the query tool. We are not inserting records in the array type column “name”, because we will copy the records of jsonb column “info” to it later. Thus, we have added the JSON data into the “info” column and integer value into the “ID” column. It was quite easy to use the “VALUES” keyword and was successful as per the below output.

To get the malformed array literal error, we must use the wrong query format in the query tool. Thus, we have been using the UPDATE instruction to modify the records of the table “Malformed”. We are using the “SET” keyword to cast the array record “name” as text from the info column to the “name” column, which is empty right now. On running this instruction, we have found that this way of copying JSON data to an array-type column is throwing an error “malformed array literal”. We have to change the format of copying the data so far.

To copy the JSONB column data to some array-type column, we need to utilize the concat function within our UPDATE command. Therefore, we used the UPDATE command to modify the table “Malformed”. The SET keyword assigns the record to column “name” of array type. While assigning, it uses concat and translates function. The translate function will convert the JSON data to array type for the column “info”. After that, the concat function will add up the translated data to one in the form of an array so that it can be saved to the column “name”. The error has been removed on execution, and data has been copied properly.

Let’s display the table “Malformed” data on our pgAdmin GUI screen using the “SELECT” instruction shown below. You can see that the JSON data from column “info” is successfully copied to the array column “name”.

Example 02:

Another way to get this error on your database is using the wrong way to merge two arrays. Thus, we will be using the SELECT ARRAY query to merge the array values 11 and 25 within square brackets to a value in single inverted commas, i.e., 78 separated by the “||” sign beneath the column “Array”. The execution of this query leads to the same errors.

To resolve this error, you need to add the value after “||” into curly brackets within the single inverted commas as ’{78}’. On execution, you will see that the array will be formed as “{11,25,78}” beneath the column “Array”.

Let’s take another illustration to get the error: malformed array literal. Thus, we have been merging the array in a square bracket with the none, i.e., empty value in single commas. On running this instruction, we have found the same malformed array literal error on the output.

To recover our system from this error, we will be replacing the empty inverted commas with the “NULL” keyword in the below-shown image. On execution of this instruction, we have got the array {11,25}’ beneath the column “Array” in the output area.

Example 03:

Let’s take the last example to get the error: malformed array literal and solve it through. Assume you have a table named “Ftest” in your database with some records in it. Fetch all its records with the SELECT instruction shown below. It is fine when you are fetching all its records without any condition as per the instruction below used within the query tool.

Let’s fetch all the records of this table from ID 1 to 4 using the WHERE clause condition. The IDs have been mentioned in the simple brackets within single inverted commas. But, it leads us to a malformed array literal error.

To resolve this error, we need to combine two conditions through AND operator within the WHERE clause of the SELECT instruction. This time, our query worked greatly and displayed the records from ID 3 to 5.


Finally! We have completed the explanation of solving the PostgreSQL error “malformed array literal”. We have discussed three of the different scenarios that may cause this error in the PostgreSQL database. We have also covered the solutions to all those scenarios that may cause this error to happen. Therefore, we know that you will find all these examples easy to understand and learn a new thing in the PostgreSQL database.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.