public inbox for [email protected]  
help / color / mirror / Atom feed
Re: psql --html and to_char()
2+ messages / 2 participants
[nested] [flat]

* Re: psql --html and to_char()
@ 2025-08-27 22:12 Greg Sabino Mullane <[email protected]>
  2025-08-28 05:31 ` Re: psql --html and to_char() Ron Johnson <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Greg Sabino Mullane @ 2025-08-27 22:12 UTC (permalink / raw)
  To: Ron Johnson <[email protected]>; +Cc: pgsql-general

On Wed, Aug 27, 2025 at 11:28 AM Ron Johnson <[email protected]>
wrote:

> Is there a way around this, other than writing my own HTMLifier?
>

Not unless your to_char() output can be coerced back into a numeric. (for
the archives sake, see fe_utils/print.c for the complete list of
right-aligned columns: int family, float, numeric, oid, xid, cid, and the
when-will-it-finally-die money type)

Cheers,
Greg

--
Crunchy Data - https://www.crunchydata.com
Enterprise Postgres Software Products & Tech Support


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: psql --html and to_char()
  2025-08-27 22:12 Re: psql --html and to_char() Greg Sabino Mullane <[email protected]>
@ 2025-08-28 05:31 ` Ron Johnson <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Ron Johnson @ 2025-08-28 05:31 UTC (permalink / raw)
  To: ; +Cc: pgsql-general

On Wed, Aug 27, 2025 at 6:13 PM Greg Sabino Mullane <[email protected]>
wrote:

> On Wed, Aug 27, 2025 at 11:28 AM Ron Johnson <[email protected]>
> wrote:
>
>> Is there a way around this, other than writing my own HTMLifier?
>>
>
> Not unless your to_char() output can be coerced back into a numeric. (for
> the archives sake, see fe_utils/print.c for the complete list of
> right-aligned columns: int family, float, numeric, oid, xid, cid, and the
> when-will-it-finally-die money type)
>

Easier to write a generic shell function.

HTMLify_csv()
{
    local -rn pFormats="$1"
    local line
    local -a lFields
    printf '<table border=1><font size="2">\n'
    read -r line
    IFS='|' read -r -a lFields <<< "${line}"
    local -i lFldCnt=${#lFields[@]}
    printf '<tr>'
    for ((i=0; i<$lFldCnt; i++)); do printf '<th align="%s">%s</th>' center
"${lFields[i]}"; done
    printf "</tr>\n"
    while IFS= read -r line
    do
        [[ $line =~ rows?\)$ ]] && { printf '<tr><td>%s</td></tr>\n'
"$line"; continue; }
        IFS='|' read -r -a lFields <<< "${line}"
        printf '<tr>'
        for ((i=0; i<$lFldCnt; i++))
        do
            printf '<td align="%s">%s</td>' "${pFormats[i]}" "${lFields[i]}"
        done
        printf "</tr>\n"
    done
    printf '</table>\n'
}

declare -a Formats=(right left)
psql -XAc "select to_char(oid::integer, '99,999') as oid, datname from
pg_database;" | HTMLify_csv Formats

It requires an array that describes how you want each column aligned.

-- 
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!


^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-08-28 05:31 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-08-27 22:12 Re: psql --html and to_char() Greg Sabino Mullane <[email protected]>
2025-08-28 05:31 ` Ron Johnson <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox