Thread: psql format result as markdown tables

psql format result as markdown tables

From
Nicolas Paris
Date:
Hello

I wonder if someone knows how to configure psql to output results as
markdown tables. 
Then instead of :

SELECT * FROM (values(1,2),(3,4)) as t;
 column1 | column2 
---------+---------
       1 |       2
       3 |       4

Get the result as :
SELECT * FROM (values(1,2),(3,4)) as t;
| column1 | column2| 
|---------|--------|-
|       1 |       2|
|       3 |       4|

Thanks by advance


Re: psql format result as markdown tables

From
Melvin Davidson
Date:


On Sat, Jan 13, 2018 at 4:50 AM, Nicolas Paris <niparisco@gmail.com> wrote:
Hello

I wonder if someone knows how to configure psql to output results as
markdown tables.
Then instead of :

SELECT * FROM (values(1,2),(3,4)) as t;
 column1 | column2
---------+---------
       1 |       2
       3 |       4

Get the result as :
SELECT * FROM (values(1,2),(3,4)) as t;
| column1 | column2|
|---------|--------|-
|       1 |       2|
|       3 |       4|

Thanks by advance


A. You have not graced us with PostgreSQL version and O/S
B. Your two examples appear exactly the same.

Found with a google search of .... sql markdown table

--
Melvin Davidson
I reserve the right to fantasize.  Whether or not you
wish to share my fantasy is entirely up to you.

Re: psql format result as markdown tables

From
Stephen Frost
Date:
Hi Nicolas!

* Nicolas Paris (niparisco@gmail.com) wrote:
> I wonder if someone knows how to configure psql to output results as
> markdown tables.

Unfortunately, I don't believe we support markdown as an output format
as yet.  To change the output format, you can use '\pset format html'.

You can see the list of formats supported by psql here:

https://www.postgresql.org/docs/current/static/app-psql.html

Look for 'format' or 'asciidoc' in that page to find the list quickly.

I suspect that if someone wanted to write the code to have markdown be
available that it would certainly be accepted, as that's definitely a
popular format.

Thanks!

Stephen

Attachment

Re: psql format result as markdown tables

From
Stephen Frost
Date:
Greetings Melvin,

* Melvin Davidson (melvin6925@gmail.com) wrote:
> On Sat, Jan 13, 2018 at 4:50 AM, Nicolas Paris <niparisco@gmail.com> wrote:
> > I wonder if someone knows how to configure psql to output results as
> > markdown tables.
> > Then instead of :
> >
> > SELECT * FROM (values(1,2),(3,4)) as t;
> >  column1 | column2
> > ---------+---------
> >        1 |       2
> >        3 |       4
> >
> > Get the result as :
> > SELECT * FROM (values(1,2),(3,4)) as t;
> > | column1 | column2|
> > |---------|--------|-
> > |       1 |       2|
> > |       3 |       4|
> >
> > Thanks by advance
>
> *A. You have not graced us with PostgreSQL version and O/S*

While it can make a difference, it doesn't in this case, as far as I can
tell.  You can see the list of formats supported by each version of
PostgreSQL by going to:

https://www.postgresql.org/docs/current/static/app-psql.html

and then you can walk backwards through the various releases.

> *B. Your two examples appear exactly the same.*

Not true, actually, if you look carefully you'll see that there's a set
of pipes down the left-hand side, and pipes all down the middle (instead
of a '+' on the linebreak between the header and the data).  Those kinds
of differences are what would make having an actual markdown output
format for psql particularly useful as, otherwise, you have to
hand-massage it or use another tool.

> *C. Have you looked at https://donatstudios.com/CsvToMarkdownTable
> <https://donatstudios.com/CsvToMarkdownTable> ?*

That might be helpful in this case, though it would require sending data
to an 3rd party, which might not be ideal.  There's a number of
csv2markdown tools out there though which could be installed locally.
Not quite as nice as having the support in psql for it though.

Thanks!

Stephen

Attachment

Re: psql format result as markdown tables

From
Tom Lane
Date:
Stephen Frost <sfrost@snowman.net> writes:
> I suspect that if someone wanted to write the code to have markdown be
> available that it would certainly be accepted, as that's definitely a
> popular format.

Somebody was working on that awhile ago,

https://www.postgresql.org/message-id/flat/CAAYBy8YU4pXYKDHeQhsA_%3DFC93yOBZp5j1h%3DBSSAo9-oLcwNww%40mail.gmail.com

Seems to have lost interest though ...

            regards, tom lane


Re: psql format result as markdown tables

From
Vick Khera
Date:
How does this work for you? I use this to get tables to insert into my wiki, which are basically the format you want. I just delete the extra lines I don't want at the end.

vk=> SELECT * FROM (values(1,2),(3,4)) as t;
 column1 | column2
---------+---------
       1 |       2
       3 |       4
(2 rows)

Time: 37.888 ms
vk=> \pset border 2
Border style is 2.
vk=> SELECT * FROM (values(1,2),(3,4)) as t;
+---------+---------+
| column1 | column2 |
+---------+---------+
|       1 |       2 |
|       3 |       4 |
+---------+---------+
(2 rows)

For you it looks like you need to change the "+" to "|" and it will work and delete the first and last lines. I don't know if you can change that with some other \pset setting.

On Sat, Jan 13, 2018 at 4:50 AM, Nicolas Paris <niparisco@gmail.com> wrote:
Hello

I wonder if someone knows how to configure psql to output results as
markdown tables.
Then instead of :

SELECT * FROM (values(1,2),(3,4)) as t;
 column1 | column2
---------+---------
       1 |       2
       3 |       4

Get the result as :
SELECT * FROM (values(1,2),(3,4)) as t;
| column1 | column2|
|---------|--------|-
|       1 |       2|
|       3 |       4|

Thanks by advance