Book Image

PostgreSQL Cookbook

By : Chitij Chauhan
Book Image

PostgreSQL Cookbook

By: Chitij Chauhan

Overview of this book

<p>PostgreSQL is an open source database management system. It is used for a wide variety of development practices such as software and web design, as well as for handling large datasets (big data).</p> <p>With the goal of teaching you the skills to master PostgreSQL, the book begins by giving you a glimpse of the unique features of PostgreSQL and how to utilize them to solve real-world problems. With the aid of practical examples, the book will then show you how to create and manage databases. You will learn how to secure PostgreSQL, perform administration and maintenance tasks, implement high availability features, and provide replication. The book will conclude by teaching you how to migrate information from other databases to PostgreSQL.</p>
Table of Contents (19 chapters)
PostgreSQL Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating tables using Perl


In this recipe, we are going to show you how to create tables in the PostgreSQL database using Perl.

Getting ready

We will be the using the qq operator, and the parameter passed to the operator will contain the CREATE TABLE SQL statement. The qq operator is used to return a double-quoted string. Before creating the table, we must first use the connect function to connect to the PostgreSQL database.

How to do it...

We can use the following code to create a table by the name EMPLOYEES. This table will be stored in the dvdrental database because the connection made by the PostgreSQL adapter is to the dvdrental database. The following code is saved in a file called createtable.pl, which will be executed later:

#!/usr/bin/perl

use DBI;
use strict;

my $driver   = "Pg"; 
my $database = "dvdrental";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432";
my $userid = "postgres";
my $password = "postgres";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError...