Melissa Melissa - 1 year ago 71
Bash Question

In Postgres psql shell, is it possible to just display the first line of a large text field?

I'm saving some huge chunks of text into a Postgres table's text field.

I'd like to see just the first 20 or so character of the text field displayed on my psql shell.

I'm currently running in psql:

select * from tablename;

and it's not showing anything in the terminal because it's too large.

The data is definitely saved, because I can display them online.

As you can see when I describe the table, the "datatext" field I'm using is a "text" type.

\d+ mytablename;
Table "public.mytablename"
Column | Type | Modifiers | Storage | Stats target | Description
id | integer | not null default nextval('mytablename_id_seq'::regclass) | plain | |
when | timestamp with time zone | not null | plain | |
datatext | text | not null | extended | |
"mytablename_pkey" PRIMARY KEY, btree (id)

Answer Source

If you only want to display the first 20 characters of text in a field, you can do this:

SELECT LEFT(datatext,20) AS First_20_Chars FROM tablename LIMIT 1;

You can control how many records return using the LIMIT statement at the end.