From 81d7c7820c25cdca723bbe7c13a3657174904b70 Mon Sep 17 00:00:00 2001 From: Silvino Silva Date: Thu, 29 Sep 2016 05:21:34 +0100 Subject: postgresql revision --- tools/conf/etc/rc.d/postgresql | 16 ++++ tools/conf/srv/pgsql/data/pg_hba.conf | 96 ++++++++++++++++++++ tools/postgresql.html | 165 ++++++++++++++++------------------ tools/scripts/install-postgres.sh | 16 ++++ 4 files changed, 203 insertions(+), 90 deletions(-) create mode 100755 tools/conf/etc/rc.d/postgresql create mode 100644 tools/conf/srv/pgsql/data/pg_hba.conf create mode 100644 tools/scripts/install-postgres.sh (limited to 'tools') diff --git a/tools/conf/etc/rc.d/postgresql b/tools/conf/etc/rc.d/postgresql new file mode 100755 index 0000000..5f0762a --- /dev/null +++ b/tools/conf/etc/rc.d/postgresql @@ -0,0 +1,16 @@ +# +# /etc/rc.d/postgresql: start, stop or restart PostgreSQL server postmaster +# + +PG_DATA=/srv/pgsql/data + +case "$1" in + start|stop|status|restart|reload) + sudo -u postgres pg_ctl -D "$PG_DATA" -l /var/log/postgresql "$1" + ;; + *) + echo "usage: $0 start|stop|restart|reload|status" + ;; +esac + +# End of file diff --git a/tools/conf/srv/pgsql/data/pg_hba.conf b/tools/conf/srv/pgsql/data/pg_hba.conf new file mode 100644 index 0000000..34587d4 --- /dev/null +++ b/tools/conf/srv/pgsql/data/pg_hba.conf @@ -0,0 +1,96 @@ +# PostgreSQL Client Authentication Configuration File +# =================================================== +# +# Refer to the "Client Authentication" section in the PostgreSQL +# documentation for a complete description of this file. A short +# synopsis follows. +# +# This file controls: which hosts are allowed to connect, how clients +# are authenticated, which PostgreSQL user names they can use, which +# databases they can access. Records take one of these forms: +# +# local DATABASE USER METHOD [OPTIONS] +# host DATABASE USER ADDRESS METHOD [OPTIONS] +# hostssl DATABASE USER ADDRESS METHOD [OPTIONS] +# hostnossl DATABASE USER ADDRESS METHOD [OPTIONS] +# +# (The uppercase items must be replaced by actual values.) +# +# The first field is the connection type: "local" is a Unix-domain +# socket, "host" is either a plain or SSL-encrypted TCP/IP socket, +# "hostssl" is an SSL-encrypted TCP/IP socket, and "hostnossl" is a +# plain TCP/IP socket. +# +# DATABASE can be "all", "sameuser", "samerole", "replication", a +# database name, or a comma-separated list thereof. The "all" +# keyword does not match "replication". Access to replication +# must be enabled in a separate record (see example below). +# +# USER can be "all", a user name, a group name prefixed with "+", or a +# comma-separated list thereof. In both the DATABASE and USER fields +# you can also write a file name prefixed with "@" to include names +# from a separate file. +# +# ADDRESS specifies the set of hosts the record matches. It can be a +# host name, or it is made up of an IP address and a CIDR mask that is +# an integer (between 0 and 32 (IPv4) or 128 (IPv6) inclusive) that +# specifies the number of significant bits in the mask. A host name +# that starts with a dot (.) matches a suffix of the actual host name. +# Alternatively, you can write an IP address and netmask in separate +# columns to specify the set of hosts. Instead of a CIDR-address, you +# can write "samehost" to match any of the server's own IP addresses, +# or "samenet" to match any address in any subnet that the server is +# directly connected to. +# +# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi", +# "ident", "peer", "pam", "ldap", "radius" or "cert". Note that +# "password" sends passwords in clear text; "md5" is preferred since +# it sends encrypted passwords. +# +# OPTIONS are a set of options for the authentication in the format +# NAME=VALUE. The available options depend on the different +# authentication methods -- refer to the "Client Authentication" +# section in the documentation for a list of which options are +# available for which authentication methods. +# +# Database and user names containing spaces, commas, quotes and other +# special characters must be quoted. Quoting one of the keywords +# "all", "sameuser", "samerole" or "replication" makes the name lose +# its special character, and just match a database or username with +# that name. +# +# This file is read on server startup and when the postmaster receives +# a SIGHUP signal. If you edit the file on a running system, you have +# to SIGHUP the postmaster for the changes to take effect. You can +# use "pg_ctl reload" to do that. + +# Put your actual configuration here +# ---------------------------------- +# +# If you want to allow non-local connections, you need to add more +# "host" records. In that case you will also need to make PostgreSQL +# listen on a non-local interface via the listen_addresses +# configuration parameter, or via the -i or -h command line switches. + +# CAUTION: Configuring the system for local "trust" authentication +# allows any local user to connect as any PostgreSQL user, including +# the database superuser. If you do not trust all your local users, +# use another authentication method. + + +# TYPE DATABASE USER ADDRESS METHOD + +# "local" is for Unix domain socket connections only +#local all all trust +local all postgres ident +# IPv4 local connections: +#host all all 127.0.0.1/32 trust +hostssl all all 127.0.0.1/32 md5 + +# IPv6 local connections: +#host all all ::1/128 trust +# Allow replication connections from localhost, by a user with the +# replication privilege. +#local replication postgres trust +#host replication postgres 127.0.0.1/32 trust +#host replication postgres ::1/128 trust diff --git a/tools/postgresql.html b/tools/postgresql.html index e160ae2..b8790e2 100644 --- a/tools/postgresql.html +++ b/tools/postgresql.html @@ -11,53 +11,22 @@

Postgresql

-

1.1. Install Postgresql

+

1. Install Postgresql

-
-        $ prt-get depinst postgresql
-        
- -

Mount Point;

- -
-        # mkdir -p /srv/pgsql
-        # mount /srv/pgsql
-        
- -

Create user;

- -
-        # useradd -U -m -d /srv/pgsql -s /bin/false postgres
-        useradd: warning: the home directory already exists.
-        Not copying any file from skel directory into it.
-        
+

Install postgresql;

-        # passwd -l postgres
-        passwd: password expiry information changed.
-        # touch /var/log/pgsql
-        # chown -R postgres:postgres /srv/pgsql /var/log/pgsql
-        # ldconfig /user/lib/postgresql
-		
- - $ sudo -u postgres mkdir -p /srv/pgsql/data - # sudo -u postgres touch /srv/pgsql/.psql_history + $ prt-get depinst postgresql -

Create /etc/logrotate.d/postgres;

-
-        /var/log/pgsql {
-            weekly
-            compress
-            delaycompress
-            rotate 10
-            notifempty
-            create 660 postgres postgres
-        }
+        # mkdir /srv/pgsql/
+        # touch /var/log/postgresql
+        # chown postgres:postgres /srv/pgsql /var/log/postgresql
+        # sudo -u postgres initdb -D /srv/pgsql/data
         
-

Edit /etc/rc.d/postgresql;

+

Change /etc/rc.d/postgresql;

         #
@@ -65,25 +34,20 @@
         #
 
         PG_DATA=/srv/pgsql/data
-        PG_HOME=/srv/pgsql
 
         case "$1" in
-        start|stop|status|restart|reload)
-                (cd $PG_HOME && sudo -u postgres pg_ctl -D "$PG_DATA" -l /var/log/pgsql "$1")
-        ;;
-        *)
-        echo "usage: $0 start|stop|restart|reload|status"
-        ;;
+            start|stop|status|restart|reload)
+                sudo -u postgres pg_ctl -D "$PG_DATA" -l /var/log/postgresql "$1"
+                ;;
+            *)
+                echo "usage: $0 start|stop|restart|reload|status"
+                ;;
         esac
 
         # End of file
         
-

1.2. Configure Server

- -
-        # sudo -u postgres initdb -D /srv/pgsql/data
-        
+

2. Configure Server

Create password for super user;

@@ -92,17 +56,9 @@ $ psql -U postgres -

Edit /pgsql/data/postgresql.conf;

- -
-        log_destination = 'syslog' # Can specify multiple destinations
-        syslog_facility='LOCAL0'
-        syslog_ident='postgres'
-        log_connections = on
-        password_encryption=on
-        
- -

Edit /srv/pgsql/data/pg_hba.conf;

+

Edit + /srv/pgsql/data/pg_hba.conf; +

         # TYPE  DATABASE        USER            ADDRESS                 METHOD
@@ -121,62 +77,89 @@
         #host    replication     postgres        ::1/128                 trust
         
+

Start server and alter postgres password

+ +
+        # /etc/rc.d/postgresql start
+        
+ +
+        postgres=# alter user postgres with password 'new_password';
+        
+ +

2.1. Configure syslog-ng

+

Configure Syslog-ng, check Michael at otacoo article. Example;

+

Edit /pgsql/data/postgresql.conf;

+
-        destination postgres { file("/var/log/pgsql"); };
-        filter f_postgres { facility(local0); };
-        log { source(s_log); filter(f_postgres); destination(postgres); };
+        log_destination = 'syslog' # Can specify multiple destinations
+        syslog_facility='LOCAL0'
+        syslog_ident='postgres'
+        log_connections = on
+        password_encryption=on
         
-

Start server and alter postgres password

+ +

Create /etc/logrotate.d/postgres;

-        # /etc/rc.d/postgresql start
+        /var/log/pgsql {
+            weekly
+            compress
+            delaycompress
+            rotate 10
+            notifempty
+            create 660 postgres postgres
+        }
         
+
-        postgres=# alter user postgres with password 'new_password';
+        destination postgres { file("/var/log/pgsql"); };
+        filter f_postgres { facility(local0); };
+        log { source(s_log); filter(f_postgres); destination(postgres); };
         
-

1.3. Create User

+ +

3. Create User

Create a new user with createuser command;

         $ sudo -u postgres createuser --pwprompt --encrypted \
-        --no-createrole --no-createdb laravel_user
+        --no-createrole --no-createdb flyspray
         Enter password for new user:
         Enter it again:
         
-

1.4. Create Database

+

4. Create Database

Create a new database for new user with createdb command;

         $ sudo -u postgres createdb --template=template0 --encoding=UTF8 \
-        --owner=laravel_user laravel_db
+        --owner=flyspray db_flyspray
         
-

1.5. Drop Database

+

5. Drop Database

Deleting database with dropdb command;

-        sudo -u postgres dropdb laravel_db
+        sudo -u postgres dropdb db_flyspray
         
-

1.6. Drop User

+

6. Drop User

Deleting user with dropuser command;

-        sudo -u postgres dropuser laravel_user
+        sudo -u postgres dropuser flyspray
         
- -

1.7. Psql

+

7. Psql

Lets check with psql, login with user postgres;

@@ -190,7 +173,7 @@ postgres=# \? -

Psql - List Databases and Roles

+

7.1. List Databases and Roles

List roles then list databases;

@@ -199,39 +182,43 @@ postgres=# \l -

Psql - Create Database

+

7.2. Create Database

-        postgres=# create database laravel_db_ext owner laravel_user encoding 'UTF-8' template template0;
+        postgres=# create database db_flyspray_ext owner flyspray encoding 'UTF-8' template template0;
         
-

Psql - Drop Tables

+

7.3. Drop Tables

This example assumes that all tables, are in public schema. First connect to database and list tables;

-        postgres=# \c laravel_db
+        postgres=# revoke SELECT on db_flyspray from flyspray;
+        
+ +
+        postgres=# \c db_flyspray
         postgres=# \dt
         

Drop all tables on public schema and recreate public schema;

-        laravel_db=# drop schema public cascade;
-        laravel_db=# create schema public;
+        db_flyspray=# drop schema public cascade;
+        db_flyspray=# create schema public;
         
-

Backup

+

7.4. Backup

Backup Database

-

Restore

+

7.5. Restore

-        $ psql laravel_db < database_dump
+        $ psql db_flyspray < database_dump
         
Tools Index @@ -242,7 +229,5 @@ See the file Gnu Free Documentation License for copying conditions.

- - diff --git a/tools/scripts/install-postgres.sh b/tools/scripts/install-postgres.sh new file mode 100644 index 0000000..06666e0 --- /dev/null +++ b/tools/scripts/install-postgres.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +. `dirname $0`/config-install.sh + +prt-get depinst postgresql + +cp -R $CONF_DIR/etc/rc.d/postgresql /etc/rc.d/ + +mkdir /srv/pgsql/ +touch /var/log/postgresql +chown postgres:postgres /srv/pgsql /var/log/postgresql + +sudo -u postgres initdb -D /srv/pgsql/data + +cp $CONF_DIR/srv/pgsql/data/pg_hba.conf /srv/pgsql/data/ +chown postgres:postgres /srv/pgsql/data/pg_hba.conf -- cgit 1.4.1-2-gfad0