Maybe the connection is falited. At least with Postgres 7.2, connecting to local postgresdatabase requires a user in the database with the same name as the user running apache, or the connection fails.
$connection = pg_connect("host=localhost port=5432 dbname=DATABASENAME user=USERNAME password=PASSWORD")
or die ("Connection error --> " . pg_last_error($conn));
You only have to pass the $connection variable if you have more than one connection to choose from (for example, if you have opened two connections with the pg_connect() function).
Also you can try to use pg_exec() :
function createTable($id, $name, $level)
{
$createS = "CREATE TABLE table_s (id SERIAL, node_name CHAR(30), parent_id SERIAL)";
$createR = pg_exec($conn, $createS);
if(!createR)
{
echo "An error occured with query.";
exit;
}else echo "Query success";
$createSt = "INSERT INTO table_s VALUES (2,'test',3)";
$createRt = pg_exec($conn, $createSt);
echo "a";
}