add postgres support.

This commit is contained in:
Miroslav Šedivý 2021-10-29 21:56:50 +02:00
parent 8ad8b5ca6f
commit 3652faf8ab
4 changed files with 135 additions and 6 deletions

View File

@ -1,7 +1,7 @@
FROM php:7.4-apache
RUN set -eux; apt-get update; \
apt-get install -y --no-install-recommends \
apt-get install -y --no-install-recommends libpq-dev \
#
# install curl
libcurl4-openssl-dev \
@ -18,7 +18,7 @@ RUN set -eux; apt-get update; \
--with-jpeg --with-webp --with-xpm --with-freetype; \
#
# install extensions
docker-php-ext-install curl gd pdo pdo_mysql exif; \
docker-php-ext-install curl gd pdo pdo_mysql pdo_pgsql exif; \
#
# set up environment
a2enmod rewrite;

View File

@ -73,12 +73,82 @@ services:
- ./data:/var/www/html/data
```
## Install standalone app using `docker-compose` with mysql
## Install standalone app using `docker-compose`
You need to install [docker-compose](https://docs.docker.com/compose/install/).
### Step 1: Download and run `docker-compose.yml`.
### MySQL
```yaml
version: "3"
services:
webserver:
image: m1k1o/blog:latest
container_name: blog_apache
environment:
TZ: Europe/Vienna
BLOG_DB_CONNECTION: mysql
BLOG_MYSQL_HOST: mariadb
BLOG_MYSQL_PORT: 3306
BLOG_MYSQL_USER: blog
BLOG_MYSQL_PASS: blog # use secure password
BLOG_DB_NAME: blog
restart: unless-stopped
ports:
- ${HTTP_PORT-80}:80
volumes:
- ${DATA-./data}:/var/www/html/data
mariadb:
image: mariadb:10.1
container_name: blog_mariadb
environment:
MYSQL_USER: blog
MYSQL_PASSWORD: blog # use secure password
MYSQL_DATABASE: blog
MYSQL_ROOT_PASSWORD: root # use secure password
restart: unless-stopped
volumes:
- mariadb:/var/lib/mysql
- ./app/db/mysql:/docker-entrypoint-initdb.d:ro
volumes:
mariadb:
```
### Postgres
```yaml
version: "3"
services:
webserver:
image: m1k1o/blog:latest
container_name: blog_apache
environment:
TZ: Europe/Vienna
BLOG_DB_CONNECTION: postgres
BLOG_POSTGRES_HOST: postgres
BLOG_POSTGRES_PORT: 5432
BLOG_POSTGRES_USER: blog
BLOG_POSTGRES_PASS: blog # use secure password
BLOG_DB_NAME: blog
restart: unless-stopped
ports:
- ${HTTP_PORT-80}:80
volumes:
- ${DATA-./data}:/var/www/html/data
postgres:
image: postgres:14
container_name: blog_postgres
environment:
POSTGRES_USER: blog
POSTGRES_PASSWORD: blog # use secure password
POSTGRES_DB: blog
restart: unless-stopped
volumes:
- postgres:/var/lib/postgresql/data
- ./app/db/postgres:/docker-entrypoint-initdb.d:ro
volumes:
postgres:
```
### Step 1: Run `docker-compose.yml`.
```sh
wget https://raw.githubusercontent.com/m1k1o/blog/master/docker-compose.yml
docker-compose up -d
```

View File

@ -41,6 +41,9 @@ class DB
case 'mysql':
$this->mysql_connect();
break;
case 'postgres':
$this->postgres_connect();
break;
case 'sqlite':
$this->sqlite_connect();
break;
@ -92,6 +95,48 @@ class DB
}
}
private final function postgres_connect(){
$host = Config::get_safe('postgres_host', false);
$port = Config::get_safe('postgres_port', false);
$socket = Config::get_safe('postgres_socket', false);
if($socket === false && $host === false){
throw new DBException("Postgres host or socket must be defined.");
}
// Try to connect
try {
$this->_PDO = new \PDO(
// Server
'pgsql:'.
($socket !== false
? 'unix_socket='.$socket
: 'host='.$host.($port !== false ? ';port='.$port : '')
).
// DB
';dbname='.Config::get('db_name').
// Charset
';options=\'--client_encoding=UTF8\'',
// Username
Config::get('postgres_user'),
// Password
Config::get_safe('postgres_pass', ''),
// Set attributes
[
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_EMULATE_PREPARES => false
]
);
$this->_PDO->exec(
// Set timezone
'SET timezone="'.date('P').'";'
);
} catch (PDOException $e) {
throw new DBException($e->getMessage());
}
}
private final function sqlite_connect(){
$sqlite_db = PROJECT_PATH.Config::get_safe('sqlite_db', "data/sqlite.db");
@ -165,6 +210,11 @@ class DB
$sql = $params[0];
unset($params[0]);
// Replace backticks with " for postgres
if(DB::connection() === 'postgres') {
$sql = str_replace("`", '"', $sql);
}
// Debug mode
if(Config::get_safe('debug', false)){
echo "<!-- ".$sql." + ".json_encode($params)." -->\n";

View File

@ -11,6 +11,15 @@ db_connection = sqlite
;mysql_pass = root
;db_name = blog
;[database]
;db_connection = postgres
;postgres_socket = /tmp/postgres.sock
;postgres_host = localhost
;postgres_port = 5432
;postgres_user = root
;postgres_pass = root
;db_name = blog
[profile]
title = Blog
name = Max Musermann
@ -63,5 +72,5 @@ logs_path = data/logs/
[system]
;timezone = Europe/Vienna
version = 1.31
debug = false
debug = true
logs = false