Solved

How to insert text file into postgresql?

Posted on 2008-10-20
1
1,706 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
[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
1 Comment
 
LVL 3

Accepted Solution

by:
pzurowski earned 0 total points
ID: 22763203
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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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