Here is a new page .
Pgbash is a useful tool which can directly execute SQL with using all the functionality of a bash shell.
|
[Japanese] [English]
pgbash-2.1 released
What is Pgbash ?
Pgbash is a shell which includes the "direct SQL" and the "embedded SQL" interface for PostgreSQL. Pgbash can be used as a log-in shell, a sub-shell(the shell started from a shell) and a shell program use.
Here, "Direct SQL" shows the functionality which immediately outputs the result to the standard output. And "Embedding SQL" shows the functionality which sets the retrieval result into the shell variable and which deals with the shell variable by using the shell script language.
SQL statement(terminal character is a semicolon) is handled as a shell command, it is possible to execute the SQL statement with pipeline, redirection and background_job options. Pgbash can provide the flexible interactive operational environment for the SQL execution. The executive example of SQL is shown next.
prompt> /usr/local/bin/pgbash --- Start Pgbash as sub-shell
pgbash> connect to db2@xxx.com user sakaida; --- connect to db2
pgbash> connect to db3@yyy.com user postgres; --- connect to db3
.......
pgbash> set connection db2; --- sets db2 as current DB
pgbash> addr='OSAKA'
pgbash> insert into test values( --- new line
> 111,'name',
> '$addr' --- use a shell valiable
> );
pgbash> select * from test limit 100; | more --- pipeline
.......
pgbash> select * from test; > /tmp/sel.dat & --- redirect + background_Job
pgbash> cat /tmp/sel.dat
.......
pgbash> set connection db3; --- sets db3 as current DB
pgbash> select * from test limit 100; | more
.......
pgbash> fc select --- edit and execute a Select
.......
pgbash> !! --- re-execute a Select
.......
pgbash> disconnect all;
pgbash> exit --- end of Pgbash
prompt>
|
It is possible that Pgbash makes a shell script to access a database by using IF/WHILE statement, etc. The example of shell script is shown next.
#!/usr/local/bin/pgbash
connect to dbname2 as db2 user postgres;
begin;
declare cur cursor for select * from test;
while [ 1 ]
do
fetch in cur into :AA :AA_IND, :BB; --- sets shell variables
if [ $SQLCODE -eq $SQL_NOT_FOUND ]; then --- end of a Fetch
break
fi
if [ $SQLCODE -eq $SQL_OK ]; then --- normal end
if [ $AA_IND -ne $SQL_NULL ]; then --- AA is not NULL
echo "$AA, $BB"
fi
fi
done
end;
disconnect db2;
Here, $SQL shell variables are reserved.
$SQLCODE --- status parameter
$SQL_OK --- NORMAL END code value
$SQL_NOT_FOUND --- NOT FOUND code value
$SQL_NULL --- NULL code value
|
Featurs
- Pgbash has the functionality which is similar to psql.
- Pgbash provides the flexible interactive operational environment by using a bash alias, function, and the history editing, etc.
- It is possible that Pgbash manipulates multiple databases by using CONNECT, DISCONNECT and SET CONNECTION.
- Pgbash has the functionality which sets the retrieval result into the shell variable by using FETCH INTO statement.
- Pgbash has a COPY function which a general user can specify a file name.
- It is possible to set CGI mode. In CGI mode, Pgbash switches the output to HTML, and read the data by GET/POST method, and read the data of HTTP_COOKIE.
- Pgbash sets "error code", "error message", "number of tuples", etc into the shell variable. Therefore, it is possible to know the condition after the SQL execution.
History
- 1999.07.01 : An interface is discussed at pgsql-jp ML.
- 1999.07.15 : KUBO Takehiro opens pqbash to the public by pgsql-jp ML.
- 1999.07.30 : The examination of Pgbash was started.
- 1999.08.31 : SAKAIDA Masaaki opens pgbash-1.0-beta to the public.
- 1999.09.04 : The examination of pgbash-1.1 was started.
- 1999.09.06 : pgbash-1.0b2 was released.
- 1999.10.01 : pgbash-1.1.1 was released.
- 1999.10.30 : pgbash-1.2.1 was released.
- 1999.10.31 : pgbash-1.2.2 (pgbash-1.2.1 bug fix)
- 1999.11.22 : pgbash-1.2.3 was released.
- 1999.12.25 : pgbash-2.0 was released (bash-2.03 main body was patched)
- 2000.01.07 : pgbash-2.0.1 was released (Add Multibyte function, Stop System mesages)
- 2000.05.16 : pgbash-2.1 was released
(adapted to both of PostgreSQL v6.5 and v7.0)
Thanks
Pgbash has been developed on the idea of Mr.KUBO Takehiro's orabash/pqbash(bash built-in command for Oracle/PostgreSQL). And, I have obtained the precious view from several people of pgsql-jp ML on the occasion of discussion of Pgbash. Here, I'd like to thank all those who have supported me.
|
SAKAIDA Masaaki <sakaida@psn.co.jp>
Last Modified at 2001/02/08
# Sorry, I am not good at English.
|