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/postgresql.html | 165 +++++++++++++++++++++++--------------------------- 1 file changed, 75 insertions(+), 90 deletions(-) (limited to 'tools/postgresql.html') 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.

- - -- cgit 1.4.1-2-gfad0