parsed.org

Tips by tag: psql

Display NULL by xinu on Jan 20, 2005 08:10 PM

If instead of a blank space you'd prefer something that tells you a particular column in a row is NULL, you can set it:

\pset null '(null)'
commandsconfigurationpostgresqlpsql
Ditch All Output Of Query by xinu on Jan 12, 2005 10:25 AM

If you want to run a query or a function and ditch the output, you can do something like this:

xinu=# select my_void_function() \g /dev/null
commandsconfigurationpostgresqlpsql
Echo Hidden by xinu on Jan 15, 2005 02:47 PM

If you want to see how a query like \du is being built:

\set ECHO_HIDDEN
analysiscommandsconfigurationpostgresqlpsqlsql
FIFOs Are Great by xinu on Jan 12, 2005 10:52 AM

If you want to tail the errors on another terminal, just push them to a fifo:

$ mkfifo pgerror
$ psql -U user dbname < ./dbfile 2> pgerror

On your other terminal:

$ tail -f pgerror

Voila! STDOUT on the main terminal, and STDERR on the secondary.

commandserrorsmkfifopipepsqlredirectshelltailterminal
Fixing Munged Template DB by xinu on Jan 12, 2005 10:24 AM

If you've munged your template1 database, you can rebuild it from the template0 database.

First, you must set the datistemplate attribute of template1 to false so we can drop it:

xinu=# UPDATE pg_database SET datistemplate = false WHERE datname = 'template1';
UPDATE 1
xinu=# DROP DATABASE template1;
DROP DATABASE

Next, re-create template1 using template0 as a template:

xinu=# CREATE DATABASE template1 WITH TEMPLATE = template0;
CREATE DATABASE

Then restore the datistemplate attribute of template1:

xinu=# UPDATE pg_database SET datistemplate = true WHERE datname = 'template1';
UPDATE 1
debugginggotchapostgresqlpsqlrecoveryrestoresqltemplate
MySQL-style output border by cygnus on Jan 12, 2005 10:25 AM

In your ~/.psqlrc, add this to emulate MySQL-style borders in query results:

\pset border 2
commandsconfigurationmysqloutputpostgresqlpsqlpsqlrc
PostgreSQL Authentication by xinu on Apr 29, 2008 08:28 PM

If you intend to use passwords for local database authentication, you'll need to make an adjustment to the pg_hba.conf file:

# TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK        METHOD

#local   all         all                                          ident sameuser
local   all         all                                          password
authenticationidentlocalpasswordpostgresqlpsql
PostgreSQL Dump & Restore by xinu on Mar 25, 2008 11:46 PM

In 7.4 (and possibly 8.1), the routine to dump and restore the entire database cluster follows:

# su - postgres
$ pg_dumpall > postgresql.dmp

Transfer the postgresql.dmp file to the target system, and then do the import:

# su - postgres
$ psql template1 < postgresql.dmp
dumpmigratepg_dumpallpostgresqlpsqlrestore
Query Timing by xinu on Jan 19, 2005 09:43 AM

If you want to see how long a query is taking to run, you can either run explain analyze or enable timing for the client by typing \timing.

analysisconfigurationdebuggingdurationoutputperformancepostgresqlpsqlruntimesqltiming
Returning Multiple Records by cygnus on Mar 22, 2005 09:10 AM

You can use RETURNS SETOF record in your plpgsql functions to return a set of rows. Here is an example of using pgsql to build a query using a table name parameter:

CREATE OR REPLACE FUNCTION test(text) RETURNS SETOF record AS '
DECLARE
  _table ALIAS FOR $1;
  _mycursor refcursor;
  _row record;
BEGIN
  OPEN _mycursor FOR EXECUTE ''SELECT * FROM '' || _table;
  FETCH _mycursor INTO _row;
  WHILE FOUND LOOP
    RETURN NEXT _row;
    FETCH _mycursor INTO _row;
  END LOOP;
  RETURN;
END
' LANGUAGE plpgsql;

Here is an example of calling such a function (the names and types of the result columns are required when returning SETOF record):

mydb=> SELECT * FROM test('mytable') AS (col1 integer, col2 text, col3 date);
functionsobscureplpgsqlpostgresqlpsqlsetofsql
Setting the Prompt in 'psql' by cygnus on Jan 14, 2005 12:34 PM

Set the PROMPT1 variable using psql prompt escape sequences:

\set PROMPT1 '[%n@%M:%/]=%# '
  • %n - User
  • %M - Host ([local] or the domain or IP address of the server)
  • %/ - Database
  • %# - # if superuser, $ if regular user.
commandsconfigurationescapepostgresqlpsqlvariables
Stop On Errors by xinu on Jan 12, 2005 11:07 AM

Set this variable in your ~/.psqlrc to stop on error when psql is used to run non-interactive scripts (e.g. cat file | psql ...):

\set ON_ERROR_STOP 1

Or use it from the command line:

$ psql ... -v ON_ERROR_STOP=1 ...
commandsconfigurationdebugginginteractivepipepostgresqlpsqlpsqlrc
RSS