Re: Combine psql command with shell script

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Combine psql command with shell script
Date: 2009-03-11 06:55:40
Message-ID: 20090311065540.GA6352@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 tables and want to user the "\copy" command to import data from 10 different text files. I can execute the "\copy" command 10 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(at)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(at)tux:~/sql-test$ ./shell.sh
CREATE TABLE
COPY 1
CREATE TABLE
COPY 1
CREATE TABLE
COPY 1
kretschmer(at)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Oleg Bartunov 2009-03-11 06:58:46 Re: tsearch2 dictionary for statute cites
Previous Message John R Pierce 2009-03-11 06:48:28 Re: Combine psql command with shell script