0

I want to save text KEEPING whitespace, but postgresql automatically deletes whitespace with a built in trim function. I want to turn the trim function off. When I save text, I want to KEEP whitespace.

My table looks like this:

create table user_chapters( 
  chapter_id serial primary key, 
  chapter_content varchar) 

I want chapter_content to keep the whitespace, but currently it is deleting all whitespace more than one. I want to be able to click the "tab" button to indent the first line of a paragraph. The html adds in whitespace to the p tag, but then postgresql deletes the whitespace.

Page COW
  • 515
  • 13
  • 31
  • Doesn't a `char()` datatype do what you want? – Gordon Linoff Nov 05 '19 at 23:58
  • 2
    Are you asking about leading (prefix) or following (suffix) whitespace? Postgre should preserve both leading and following whitespace but will ignore following whitespace when performing text comparisons using most collations - this is an ISO SQL standards compliance rule. See: https://til.hashrocket.com/posts/ec707a5ddf-postgres-varchar-and-silent-white-space-trimming – Dai Nov 06 '19 at 00:00
  • 1
    Please post the exact data-types of your table columns and the queries/statements you're using where you're seeing this auto-trim behaviour. `varchar` and `char` behave differently, for example. – Dai Nov 06 '19 at 00:00
  • My table looks like this: create table user_chapters( chapter_id serial primary key, chapter_content varchar) I want chapter_content to keep the whitespace, but currently it is deleting all whitespace more than one. I want to be able to click the "tab" button to indent the first line of a paragraph. The html adds in whitespace to the

    tag, but then postgresql deletes the whitespace.

    – Page COW Nov 06 '19 at 12:09
  • Unclear. In case of HTML browser itself ignores whitespace, unless you use
     tags.
    – Arvo Nov 06 '19 at 12:16
  • 1
    Postgres **will** keep the whitespace: https://rextester.com/KVBH6433 There is something in your code that removes it. –  Nov 06 '19 at 12:26
  • Arvo - thank you! It looks like that's the problem. When I inspected the html, it does in fact keep the whitespace. It's the html browser that is ignoring the whitespace. – Page COW Nov 06 '19 at 12:26
  • @PageCOW The convention here is to keep Answers separate from Questions, even if you find the solution yourself. So rather than editing the question, you're encouraged to fill in the Answer box below. – IMSoP Nov 06 '19 at 12:43
  • @IMSoP thanks! Just made the update. – Page COW Nov 06 '19 at 12:47

1 Answers1

0

@Arvo answer: postgresql DOES store the whitespaces if you use varchar. It was my html browser that was automatically removing whitespace. You have to put the text within pre tags to keep the whitespace in your html.

Page COW
  • 515
  • 13
  • 31