Create a Multi-Dimensional Array from a Query in PostgreSQL

In PostgreSQL, we can use array constructors to create arrays that contain elements that we specify. We can populate the array from literal values, or we can let a query provide the values.

We can also create multi-dimensional arrays. When populating the array from a query, we can use multiple subqueries to create each sub-array. Therefore we can create a multi-dimensional array from a query.

Continue reading

Fix Error “cannot take logarithm of a negative number” in PostgreSQL

If you’re getting an error that reads “ERROR: cannot take logarithm of a negative number” when using either the log() function or log10() function in PostgreSQL, it’s probably because you’re passing a negative value to the function.

These functions require a value greater than zero. This is true even for the base argument of the log() function (the argument that specifies which base to use).

To fix this issue, be sure to pass a value greater than zero to these functions.

Continue reading

Fix ‘time zone … not recognized’ in PostgreSQL

If you’re getting an error that reads something like ‘time zone “US/New_York” not recognized‘ in PostgreSQL while using a function such as date_add(), date_subtract() or date_trunc(), it’s probably because you’re specifying an unsupported time zone.

If you specify a time zone, it must be one that’s recognised by the system.

To fix this issue, either provide a recognised time zone or omit the time zone argument altogether (assuming this is a valid option for the situation).

Continue reading

Fix “function array_shuffle(numeric) does not exist” in PostgreSQL

If you’re getting an error that reads “function array_shuffle(numeric) does not exist” when using the array_shuffle() function in PostgreSQL, it’s probably because the argument you’re passing to the function is not an array.

More specifically, this error message implies that you’re passing a numeric type. The same error can occur when passing other non-array types (such as integer), but the solution is the same.

The argument for this function must be an array.

To fix this error, replace the numeric value with an array value when calling the array_shuffle() function.

Continue reading

Fix Error “cannot determine type of empty array” in PostgreSQL

If you’re getting an error that reads “cannot determine type of empty array” in PostgreSQL, it could be that you’re trying to create an empty array without specifying the array type.

It’s impossible to create an array with no type, and so if we want to create an empty array, then we need to specify the type or add items to determine the type.

To fix this issue, either specify the array type for the empty array, or add items to the array.

Continue reading

Fix Error “time field value out of range” when using make_time() in PostgreSQL

If you’re getting an error that reads ‘time field value out of range‘ in PostgreSQL while using the make_time() function, it’s probably because one or more of the time parts you’re providing is out of the accepted range for that time part.

To fix this issue, be sure that each time part you provide is within the valid range for that time part.

Continue reading

Fix Error “column … cannot be cast automatically to type …” in PostgreSQL

If you’re getting an error that reads something like ‘column “c1” cannot be cast automatically to type text[]‘ in PostgreSQL, it could be that you’re trying to change a column’s type to one where the existing type can’t be implicitly cast to.

To fix this issue, try explicitly casting the column to the desired data type.

Continue reading