Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display column headers returned by PostgreSQL function?

I have the following PostgreSQL function which returns multiple columns from a table:

CREATE OR REPLACE FUNCTION userbyid(id integer)
RETURNS TABLE(id int, username character varying(30), email character varying(254), last_login timestamp with time zone) AS
$$
SELECT
    id,
    username,
    email,
    last_login
FROM
    auth_user
WHERE
    id = $1;
$$
LANGUAGE 'sql';

The result it returns looks like this:

                        userbyid
--------------------------------------------------------------
(2, smith, [email protected], "2017-06-04 19:47:49.472259+00")

Is it possible to display the output with the correct column headers, something like this:

id         username       email               last_login
--------------------------------------------------------------
2          smith          [email protected]   2017-06-04

I'm looking at the CREATE FUNCTION document page and it isn't clear how to do this. I've also searched online and haven't seen an article that discusses this.

like image 385
Jim Avatar asked Jun 22 '26 04:06

Jim


1 Answers

You need to format PostgreSQL to show the colum headers; Set it with:

\t

this can be seen in PSQL help

\?

like image 127
Isaac Borbon Miquirray Avatar answered Jun 23 '26 16:06

Isaac Borbon Miquirray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!