Solved

How to insert text file into postgresql?

Posted on 2008-10-20
1
1,697 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query to calculate avaerage 21 58
Filtering characters in an SQL field 2 21
IIF in access query 19 42
SQL Server syntax 11 41
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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

730 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