0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VPSを借りる [PostgreSQL] #4

Posted at

#PostgreSQL
※How to Install PostgreSQL on CentOS 8

DOMAIN=yourdomain

PORT=5432
DATABASE=yourdb
USERNAME=yourname
PASSWORD=yourpass

sudo dnf install @postgresql postgresql-contrib

sudo postgresql-setup --initdb --unit postgresql
sudo systemctl enable --now postgresql

sudo -u postgres -i psql -c "ALTER ROLE postgres WITH PASSWORD '$PASSWORD';"

sudo -u postgres -i psql -c "CREATE ROLE $USERNAME WITH LOGIN PASSWORD '$PASSWORD';"
sudo -u postgres -i psql -c "CREATE DATABASE $DATABASE;"
sudo -u postgres -i psql -c "GRANT ALL PRIVILEGES ON DATABASE $DATABASE TO $USERNAME;"

sudo -u postgres tee /var/lib/pgsql/.pgpass << EOF
*:$PORT:*:postgres:$PASSWORD
*:$PORT:$DATABASE:$USERNAME:$PASSWORD
EOF

sudo chmod 600 /var/lib/pgsql/.pgpass

sudo cp -p /var/lib/pgsql/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf.origin
sudo sed -i -e "s/^local\s.*all\s.*all\s.*peer/local   all             all                                     md5/g" /var/lib/pgsql/data/pg_hba.conf
sudo diff -u /var/lib/pgsql/data/pg_hba.conf.origin /var/lib/pgsql/data/pg_hba.conf

sudo systemctl restart postgresql

sudo -u postgres -i psql -U $USERNAME -d $DATABASE -c "CREATE TABLE staff (id CHAR(4) NOT NULL, name TEXT NOT NULL, age INTEGER, PRIMARY KEY (id));"
sudo -u postgres -i psql -U $USERNAME -d $DATABASE -c "INSERT INTO staff VALUES (1, 'Yamada Taro', 26);"
sudo -u postgres -i psql -U $USERNAME -d $DATABASE -c "INSERT INTO staff VALUES (2, 'Suzuki Hanako', 21);"
sudo -u postgres -i psql -U $USERNAME -d $DATABASE -c "SELECT * FROM staff;"

sudo dnf install php-pgsql

sudo tee /var/www/$DOMAIN/public_html/dbtest.php << EOF
<?php
  \$db = pg_connect("dbname=$DATABASE user=$USERNAME password=$PASSWORD");
  if(!\$db){
    echo "Error: Unable to open database.<br />\n";
    exit;
  } else {
    echo "Opened database successfully.<br />\n";
  }

  \$result = pg_query(\$db, "SELECT * FROM staff"); 
  if(!\$result) {
    echo "Error: Unable to select table.<br />\n";
    exit;
  }

  while (\$row = pg_fetch_row(\$result)) {
    echo "\$row[0], \$row[1], \$row[2]<br />\n";
  }

  pg_close(\$db);
?>
EOF

curl https://$DOMAIN/dbtest.php

VPSを借りる [VPS、SSH、Nginx] #1
VPSを借りる [HTTPS] #2
VPSを借りる [PHP] #3

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?