日記

Perlのデータベース接続PostgreSQL

 

rootユーザになり以下のコマンドでPostgreSQLをアクセスするためのPerlモジュールをインストールすることができます。

install perl-DBI perl-DBD-Pg

 

PostgreSQLに接続し現在の日付を表示するサンプルコード

 

#!/usr/bin/perl

 

use DBI;

 

# PostgreSQL

our $DB_NAME = “postgres”;

our $DB_USER = “sakura”;

our $DB_PASS = “sakura”;

our $DB_HOST = “127.0.0.1”;

our $DB_PORT = “5432”;

 

my $dbh = DBI->connect(“dbi:Pg:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT”,”$DB_USER”,”$DB_PASS”) or die “$!\n Error: failed to connect to DB.\n”;

my $sth = $dbh->prepare(“SELECT now();”);

$sth->execute();

while (my $ary_ref = $sth->fetchrow_arrayref) {

my ($row) = @$ary_ref;

print $row , “\n”;

}

$sth->finish;

$dbh->disconnect;

 

1;

 

 

Last Updated on 2025年5月11日1:57 pm by cgishop

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny