Solved

Batch File

Posted on 2007-11-15
7
445 Views
Last Modified: 2013-12-06
Is there a way to write a batch file that can be scheduled in PostGre running on Red Hat Linux environment ?, I am tyring to run the copy command to import a CSV file into PostGre on a daily basis.
0
Comment
Question by:mayaldi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 14

Accepted Solution

by:
arrkerr1024 earned 250 total points
ID: 20296397
Do you mean PostgreSQL?  If so then SURE.  Just make a bash script to run postgres with the necessary commands to import the csv, and then schedule it using crontab.  Cron is the (typical) linux scheduler.  See the man pages for exact usage, but you pretty much just type "crontab -e" and then tell it what days/times to run a command.
0
 

Author Comment

by:mayaldi
ID: 20296411
yes, PostgreSQL ... Can you give me an example on how to write a script  ?
0
 
LVL 62

Assisted Solution

by:gheist
gheist earned 250 total points
ID: 20300730
Just a normal shell script invoking psql with postgresql functions called

#!/bin/sh
/usr/bin/psql <<EOT
vacuum full analyze verbose;
EOT
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 20334576
   Hi!

You could also use the COPY command as shown here
http://www.java2s.com/Code/PostgreSQL/Table/CopyinganASCIIfiletodatatable.htm

Regards,
   Tomas Helgi
0
 
LVL 10

Expert Comment

by:adrpo
ID: 20749000

Hi,

You should just give the points to arrkerr1024 and gheist.
Here is my version of the same thing with more explanations.
More about crontab here:
http://www.adminschoice.com/docs/crontab.htm

01. log as the postgres user:
      as root: $ su postgress
02. create the script:
      make a directory for script
      $ cd /home/postgress
      $ mkdir bin
      $ cd bin
      $ cat > copy-script.sh
         #!/bin/sh
         /usr/bin/psql "copy tablename from '/absolute/path/to/file.sql' with delimiter ';' CSV;"
        press CTRL+D here to end the cat command
      Now you have a file copy-script.sh. Make it executable:
      $ chmod +x copy-script.sh
03. edit the crontab:
      make sure the editor is emacs, as you may have problems with vi if you don't know how
      to use it.
      $ export EDITOR=emacs
      $ crontab -e  
         Now the editor is opened, write in there:
         0 22 * * * /home/postgres/bin/copy-script.sh
         Save the file Ctrl+X then Ctrl+S
         Exit from Emacs: Ctrl+X then Ctrl+C

The copy-script.sh will run each day at 22:00. You can change that by running
crontab -e again and change the values in there. The values mean, see below.

Cheers,
za-k/

*     *   *   *    *  /path/to/the/scheduled/script.sh
-     -   -   -    -
|     |   |   |     |
|     |   |   |     +----- day of week (0 - 6) (Sunday=0)
|     |   |   +------- month (1 - 12)
|     |   +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

Open in new window

0
 
LVL 1

Expert Comment

by:Computer101
ID: 21198044
Forced accept.

Computer101
EE Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order for businesses to be compliant with certain information security laws in some countries, you need to be able to prove that a user (which user it was becomes important to the business to take action against the user after an event has occurr…
In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question