# man > Apache::Session::Postgres

---
type: CommandReference
command: Apache::Session::Postgres
mode: perldoc
section: 3pm
source: perldoc
---

## Quick Reference
- `tie %hash, 'Apache::Session::Postgres', $id, { DataSource => 'dbi:Pg:dbname=sessions', UserName => $user, Password => $pass, Commit => 1 }` — open a new session with a fresh database connection.
- `tie %hash, 'Apache::Session::Postgres', $id, { Handle => $dbh, Commit => 1 }` — use an existing database handle to manage a session.

## Name
`Apache::Session::Postgres` — an implementation of `Apache::Session` that uses a Postgres backing store with no locking.

## Synopsis
perl
use Apache::Session::Postgres;

# New database handle
tie %hash, 'Apache::Session::Postgres', $id, {
    DataSource => 'dbi:Pg:dbname=sessions',
    UserName   => $db_user,
    Password   => $db_pass,
    Commit     => 1
};

# Existing database handle
tie %hash, 'Apache::Session::Postgres', $id, {
    Handle => $dbh,
    Commit => 1
};
## Options
- `DataSource` — DBI connection string (e.g., `dbi:Pg:dbname=sessions`). Required when no `Handle` is given.
- `UserName` — database username. Used with `DataSource`.
- `Password` — database password. Used with `DataSource`.
- `Handle` — an already‑opened `DBI` database handle. Overrides `DataSource`/`UserName`/`Password`.
- `Commit` — **mandatory**. Set to 1 to commit the transaction after session operations; set to 0 to leave transaction control to the caller. This prevents interference with local transaction policies and connection caching.

## Examples
perl
# Session with a new connection
my %session;
tie %session, 'Apache::Session::Postgres', undef, {
    DataSource => 'dbi:Pg:dbname=myapp',
    UserName   => 'app_user',
    Password   => 'app_pass',
    Commit     => 1
};
$session{user} = 'alice';
untie %session;
perl
# Session using an existing DBI handle
my $dbh = DBI->connect('dbi:Pg:dbname=myapp', 'app_user', 'app_pass');
tie my %session, 'Apache::Session::Postgres', $session_id, {
    Handle => $dbh,
    Commit => 0   # transaction handled externally
};
print $session{user};
untie %session;
$dbh->commit;
## See Also
- [Apache::Session::Store::Postgres](https://metacpan.org/pod/Apache::Session::Store::Postgres)
- [Apache::Session](https://metacpan.org/pod/Apache::Session)
- [Apache::Session::File](https://metacpan.org/pod/Apache::Session::File)
- [Apache::Session::Flex](https://metacpan.org/pod/Apache::Session::Flex)
- [Apache::Session::DB_File](https://metacpan.org/pod/Apache::Session::DB_File)