Re: Combine psql command with shell script - Mailing list pgsql-general

From A. Kretschmer
Subject Re: Combine psql command with shell script
Date
Msg-id 20090311065540.GA6352@a-kretschmer.de
Whole thread Raw
In response to Combine psql command with shell script  (John Wang <johnw822003@yahoo.com>)
List pgsql-general
In response to John Wang :
>
> How to combine psql commands, such as "\copy", with shell script? Is there any sample code? For example, I have 10
tablesand want to user the "\copy" command to import data from 10 different text files. I can execute the "\copy"
command10 times. But it is not convenient. 
>
> Thanks.

A very simple example, a shell-script to create 3 tables, create 3
text-files for COPY, and copy that files into the 3 tables:

kretschmer@tux:~/sql-test$ cat shell.sh
#!/bin/bash

for i in `seq 1 3` ; do
  psql test -c "create table table$i(i int);";
  echo -e "$i\n\\." > copy$i.copy;
  psql test -c "copy table$i from '/home/kretschmer/sql-test/copy$i.copy'";
done;


kretschmer@tux:~/sql-test$ ./shell.sh
CREATE TABLE
COPY 1
CREATE TABLE
COPY 1
CREATE TABLE
COPY 1
kretschmer@tux:~/sql-test$ psql test
Welcome to psql 8.3.6, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

test=# select * from table3;
 i
---
 3
(1 row)

test=*#



Hope that helps, Andreas
--
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net

pgsql-general by date:

Previous
From: John R Pierce
Date:
Subject: Re: Combine psql command with shell script
Next
From: Oleg Bartunov
Date:
Subject: Re: tsearch2 dictionary for statute cites