PostgreSQL CLI set max colum width - postgresql

How to set maximum column width for postgresql query result in the Linux terminal?
I know you can use:
\pset format wrapped
\pset columns 20
But it doesn't work when the output contains two columns like in this example:
select 'some long long long long text', 'some other long long long text';

After playing around a little bit, I think you are running afoul of this:
Note that psql will not attempt to wrap column header titles; therefore, wrapped format behaves the same as aligned if the total width needed for column headers exceeds the target.
Since your default column headers are 8 characters, plus (with the default settings) a mandatory white space before and after and a mandatory pipe character between them, that gives 21 columns. So it reverts to not wrapping.

Related

In PostgreSQL, is it possible to have a default format for real columns?

In PostgreSQL, I have a column with people's height in meters. If the height is, say 1.75 m, it shows properly, but if the height is 1.70 m, it shows as 1.7. I would like to have this already formatted to two decimal places, showing as 1.70 without formatting in each and every SQL call. Can I specify this in the table creation? Or a stored procedure, or something? I've seen a few things about timestamps, but not for real fields. Knowing how to format the decimal point as a colon (1,70) would be a plus.
Basically, presentation and "cosmetics" are the job of the application, not the database.
Having a default number of decimal places for floats would also create a problem, because the data returned by the database would not be the actual data in the column. So if you did a SELECT and it returned a value of 1.75, then if you searched for this value, you might not find it because the actual value stored was not 1.75 but 1.7499999999 and it was only rounded for display.
Potential solutions:
If you want to store a specified number of digits, use NUMERIC. This will solve the 1.7499999999 problem above. If you use NUMERIC, when doing a SELECT you get the actual contents of the column.
In your app, if you use an ORM, use a Decimal (or similar) type for the column with the appropriate settings so it displays the way you want.
Or create a view with the format applied to the column, but in this case if you want the trailing zero, the type will be text and not float, and it will not be searchable unless you create an extra index on it.
Generated column with the number formatted as you want, maybe easier than a view

Is there a way to limit default column width?

In Oracle SQL Developer selecting VARCHAR2, CLOB or LONG columns shows all its contents by default. Is there a way to limit this default to e.g. 20 characters, or at least not allowing columns to be bigger than the screen width?
I know I can use Auto-fit all columns → on Header, but it limits the size of columns usually too much. However, it is still easier to expand a small column, than to contract one that spans several screen widths.
This is the default view for version 20.2 - note the CLOB doesn't display in full nor take the entire screen, it leaves room for the other columns.
You could of course always use the SUBSTR() function on your CLOBs to determine exactly how much data to show for casual browsing.

I always get a value of 255 characters

I have a table in my Postgres DB. In this table, there is a column city this column has type character and length 255.
When I try to add a city in this column, for example, London and after that, I try to get this city I get a value with 255 lengths.
Looks likes [London....................-255] where dots are empty characters
When I add value in db always doing trim.
I use pg for node js
As the comment says, you don't want to use character(255) as the field type, which is always 255 characters, padded with whitespace.
Instead, you might consider using varchar(255), but even so, you probably don't actually want to limit the length here – Postgres doesn't care, storage-wise!, whereas MySQL does – so just use text.

Is there a way to set the max width of a column when displaying JSONB results in psql?

I have a problem that is somewhat similar to this question: Is there a way to set the max width of a column when displaying query results in psql?. I have a number of tables in Postgres with large JSONB documents. When I use psql from Emacs, it grinds to a halt trying to display fields with these documents. Ideally, I just want to see the first X characters of a document when I select * from a given table. I tried:
\pset columns 20
To no avail. Is there some permutation of columns and format that could achieve this? At present I am manually casting columns like so:
cast("PAYLOAD" as varchar(50))
This works, but it does mean I need to remember to cast before selecting which is a major pain.

Getting NULL Value in Stored Procedure TEXT Column

Below Query, I am using to get the SP definition but in TEXT column I am getting as NULL Value in IBM DATA Studio but I am able to CALL the SP.
SELECT PROCNAME, TEXT FROM SYSCAT.PROCEDURES WHERE PROCNAME LIKE '%USP_ABC%'
Please Help
You have confirmed that the syscat.procedures.language is SQL, and that your query-tool is able to display a substr() of the text.
Workaround depends on the length(text) of the row of interest:
SELECT PROCNAME, substr(TEXT,1, 1024) FROM SYSCAT.PROCEDURES WHERE PROCNAME LIKE '%USP_ABC%'
You may need to adjust the length of the substr extract depending on the length of the text and your configuration. For example substr(TEXT, 1, 2048 ) or a higher value for the length as necessary that your query-tool can cope with.
You can find the length of the text column with the LENGTH(TEXT) for the row of interest.
You can also CAST a CLOB to char or varchar to a length that fits within their limits and whatever query tool limitations you have.
Another option is to use a different query tool that can work with CLOB.
Are you using the latest version of Data Studio with the latest fix? It sounds like you might have an invalid UTF-8 character in you SP, or as you are using SUBSTR and SUBSTRING you are breaking a mulit-byte character in two.
You could try setting
-Ddb2.jcc.charsetDecoderEncoder=3
in your eclipse.ini to get Java to use a replacment character rather than replace the invalid string with nul
See this tech note
https://www-01.ibm.com/support/docview.wss?uid=swg21684365
Otherwise, do raise this with IBM Suppport