Hi Alex, thanks a lot for your help so far! I really appreciate all your suggestions.
I’ve followed the indicated steps and haven’t found anything unusual.
(Important: .pgpass needs a newline character at the end of the file — this isn’t documented by PostgreSQL!)
I created a very simple PHP file and placed it in /var/www/html/. Then I assigned permissions like so:
sudo chown www-data:www-data /var/www/html/testconnexio3.php
sudo chmod 644 /var/www/html/testconnexio3.php
Here is the code I used:
<?php
// Connection string without a password, because it will be read from .pgpass
$connString = "host=localhost dbname=dedalo_xxx user=myuser";
// Attempt to connect
$conn = pg_connect($connString);
if (!$conn) {
die("Error connecting to the database: " . pg_last_error() . "\n");
}
echo "Connection established successfully!\n\n";
// 1) Create a test table if it doesn't exist
$createTableSql = "
CREATE TABLE IF NOT EXISTS public.prova (
id SERIAL PRIMARY KEY,
nom TEXT NOT NULL,
edat INT
);
";
$resultCreate = pg_query($conn, $createTableSql);
if (!$resultCreate) {
die("Error creating the table: " . pg_last_error($conn) . "\n");
}
echo "Table 'prova' created (or it already existed).\n\n";
// 2) Insert some sample data (add a couple of example records)
$insertSql = "
INSERT INTO public.prova (nom, edat)
VALUES
('John Doe', 30),
('Maria Lluna', 25)
RETURNING id;
";
$resultInsert = pg_query($conn, $insertSql);
if (!$resultInsert) {
die("Error inserting data: " . pg_last_error($conn) . "\n");
}
$numInserted = pg_num_rows($resultInsert);
echo "$numInserted new records were inserted into the 'prova' table.\n\n";
// 3) Read the data to verify
$querySelect = "SELECT * FROM public.prova ORDER BY id;";
$resultSelect = pg_query($conn, $querySelect);
if (!$resultSelect) {
die("Error querying the table: " . pg_last_error($conn) . "\n");
}
echo "Records found in 'public.prova':\n";
while ($row = pg_fetch_assoc($resultSelect)) {
echo "ID: {$row['id']}, Nom: {$row['nom']}, Edat: {$row['edat']}\n";
}
// Close the connection
pg_free_result($resultSelect);
pg_close($conn);
?>
The output of this file shows that I can connect, write, and read from the database using .pgpass:
Connection established successfully! Table 'prova' created (or it already existed). 2 new records were inserted into the 'prova' table. Records found in 'public.prova': ID: 1, Nom: John Doe, Edat: 30 ID: 2, Nom: Maria Lluna, Edat: 25
However, during the installer process, I still get this message:
Error. Database import failed! Verify your .pgpass file and look for errors in php error file. - PHP get_current_user: myuser - PHP whoami: www-data - PHP home: /var/www - .pgpass file permissions: 0600
I’m not sure if it’s a file permission issue. I haven’t created any specific new linux user. Maybe I should make all the files owned by www-data? Or maybe I must delete the entire folder /var/www/html/dedalo/ and start from 0 (again).
Any advice or pointers would be greatly appreciated. Thank you all in advance for your help!