How to Use the Built-In PostgreSQL Module in ServBay
As a powerful integrated web development tool, ServBay comes with a built-in PostgreSQL module, and its activation process is very simple. PostgreSQL is a powerful, open-source object-relational database system that is widely used in modern web development. With ServBay, developers can easily enable the PostgreSQL module to use PostgreSQL for data storage and management in PHP applications.
Introduction to the PostgreSQL Module
PostgreSQL is an advanced open-source object-relational database system known for its high performance, reliability, and rich features. It supports standard SQL syntax and extends many advanced features such as complex queries, transactions, foreign keys, triggers, and views.
Main Features
- High Performance: PostgreSQL provides efficient data read and write capabilities through optimized query execution and indexing mechanisms.
- Powerful Query Capabilities: PostgreSQL supports complex queries and data operations to meet various business needs.
- Data Integrity: PostgreSQL supports transactions, foreign keys, and constraints to ensure data consistency and integrity.
- Extensibility: PostgreSQL supports stored procedures, custom functions, and various extensions, making it convenient for developers to extend database functionality.
- High Availability: PostgreSQL supports streaming replication and hot backups, providing high availability and data redundancy.
Versions of the Built-In PostgreSQL Module in ServBay
ServBay supports multiple PHP versions and pre-installs and enables the corresponding PostgreSQL module by default for each version.
How to Enable the PostgreSQL Module
By default, the PostgreSQL module is enabled with no extra configuration needed.
Using PostgreSQL in PHP Code
Once the PostgreSQL module is enabled, you can use the PostgreSQL client for database operations in PHP code. Below are two simple examples, one using the standard pg_connect method, and the other using PDO.
Example Code (Standard pg_connect Method)
<?php
// Connect to PostgreSQL database
$conn = pg_connect("host=localhost dbname=servbay_db user=servbay_user password=your_password");
if (!$conn) {
die("Could not connect to PostgreSQL");
}
// Insert data
$query = "INSERT INTO users (name, email, age) VALUES ('ServBay', '[email protected]', 30)";
$result = pg_query($conn, $query);
if ($result) {
echo "Data inserted successfully";
} else {
echo "Error in inserting data: " . pg_last_error($conn);
}
// Query data
$query = "SELECT * FROM users WHERE name = 'ServBay'";
$result = pg_query($conn, $query);
if ($result) {
$data = pg_fetch_all($result);
echo "Queried data: ";
print_r($data);
} else {
echo "Error in querying data: " . pg_last_error($conn);
}
// Close database connection
pg_close($conn);
?>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Example Code (PDO Method)
<?php
try {
// Create a new PDO instance
$dsn = 'pgsql:host=localhost;dbname=servbay_db';
$username = 'servbay_user';
$password = 'your_password';
$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$pdo = new PDO($dsn, $username, $password, $options);
// Insert data
$stmt = $pdo->prepare("INSERT INTO users (name, email, age) VALUES (:name, :email, :age)");
$stmt->execute([
':name' => 'ServBay',
':email' => '[email protected]',
':age' => 30
]);
echo "Data inserted successfully";
// Query data
$stmt = $pdo->prepare("SELECT * FROM users WHERE name = :name");
$stmt->execute([':name' => 'ServBay']);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "Queried data: ";
print_r($data);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
// Close database connection (PDO will automatically close the connection)
?>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Conclusion
ServBay provides a convenient way to manage and enable the PostgreSQL module. With simple configuration and restart operations, developers can quickly enable the PostgreSQL module in different PHP versions, allowing PostgreSQL to be used for data storage and management in PHP applications. PostgreSQL's high performance, powerful query capabilities, and data integrity make it an essential database solution in modern web development. With ServBay and PostgreSQL, developers can build efficient and reliable web applications.