> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connecting to PostgreSQL using Perl

> Learn to connect to a PostgreSQL database using Perl's DBI module with this concise guide.

This article describes how to connect to a PostgreSQL database using Perl's DBI (Database Interface) module.

<Tip>
  The PostgreSQL databases and users must already exist before you can use this method. For information about how to manage PostgreSQL databases using cPanel, please see [this article](/docs/managing-postgresql-databases).
</Tip>

## Connecting to PostgreSQL using the DBI (Database Interface) module

The DBI module enables your Perl scripts to connect to PostgreSQL databases.

To connect to PostgreSQL using the DBI module, follow these steps:

1. Use the following Perl code to connect to PostgreSQL and select a database. Replace ***username*** with your username, ***password*** with your password, and ***dbname*** with the database name:

```perl theme={null}
use DBI;
$myConnection = DBI->connect("DBI:Pg:dbname=dbname;host=localhost", "username", "password");
```

2. After the code connects to PostgreSQL and selects the database, you can run SQL queries and perform other operations. For example, the following Perl code runs a SQL query that extracts the last names from the *employees* table, and stores the result in the *\$result* variable:

```perl theme={null}
$query = $myConnection->prepare("SELECT lastname FROM employees");
$result = $query->execute();
```

## More information

To view the online documentation for Perl's DBI, please visit [http://dbi.perl.org/docs](http://dbi.perl.org/docs).

## Related articles

* [Connecting to PostgreSQL using psql](/docs/connect-to-postgresql-from-the-command-line)
