postgres cheat sheet

Creating user

sudo -u postgres createuser $user
sudo -u postgres createdb $user
sudo -u postgres psql -c "alter user $user with encrypted password '$password';"

Check the config

cat /etc/postgresql/11/main/pg_hba.conf | grep -qe "^\slocal\sall\sall\smd5\

PHP example

$conn = pg_connect("host=localhost port=5432 dbname=sakkie user=sakkie password=sakkie123"); if(!$conn) { echo "Unable to connect to the database"; } pg_prepare($conn,"my_query",'INSERT INTO TAFEL (text) values($1)'); $result = pg_execute($conn,"my_query",array($_SERVER[REMOTE_ADDR])); $result = pg_query($conn, "SELECT * FROM TAFEL"); if (!$result) { echo "An error occurred.\n"; exit; } $arr = pg_fetch_all($result); echo "
\n";
print_r($arr);
echo "
\n";


$result = pg_execute($conn,”my_query”,array($_SERVER[REMOTE_ADDR]));

$result = pg_query($conn, “SELECT * FROM TAFEL”);
if (!$result) {
echo “An error occurred.\n”;
exit;
}

$arr = pg_fetch_all($result);

\n";
print_r($arr);
echo "

\n”;