Solved

How to insert text file into postgresql?

Posted on 2008-10-20
1
1,661 Views
Last Modified: 2012-05-05
I have simple table described below. How I should form INSERT statement if I want to put a content of a file into TEXT field?

INSERT INTO foo( `path`, `content` ) VALUES( '/etc/motd', SOME_FUNCTION_PERHAPS( '/etc/motd' ) );

I will perform full-text searching on column `content` followed by thousands of INSERTs.

CREATE TABLE foo (

	path VARCHAR(255) PRIMARY KEY,

	content TEXT NULL

);

Open in new window

0
Comment
Question by:pzurowski
1 Comment
 
LVL 3

Accepted Solution

by:
pzurowski earned 0 total points
Comment Utility
I did it myself...
#!/usr/bin/perl

use DBI;
 

sub get_file_contents

{

        my $filename = @_;

        my $result;

        if( ( -e "$filename" ) > 0 )

        {

                open( TFILECNTS, "$filename" ) || die "$filename";

                read( TFILECNTS, $result, -s "$filename" );

                close( TFILECNTS );

        }

        return $result;

}
 

my $dbh = DBI->connect("DBI:Pg:");

$dbh->do("INSERT INTO rfc( path, data ) VALUES( ?, ? )", undef, ("motd", get_file_contents( "/etc/motd" ) ) );

$dbh->disconnect();

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now