Sunday, 15 January 2017

Smart use of cmd in windows

The Command Prompt aka cmd is one of the core components of the Microsoft Windows OS. It is also one of the most useful parts of Windows. It lets you perform several tasks which cannot be accomplished easily using the GUI. In fact, it won’t be an exaggeration if we call it the real Control Panel of Windows. Despite its significance, many Windows users are still unaware about most of its powerful features. Therefore, we deemed it appropriate to list the Top 10 cmd tricks every Windows user MUST know !!!

Abort a running command

Did you know you can stop a command running in the Command Prompt before it completes its task? Well, you can do this by pressing Ctrl + C during the execution of the command.

Save the output of a command to a file

Sometimes you may need to save the output of a command to a file for future reference, or maybe for posting it in some forum for getting help from others regarding the error(s) displayed in the output of that command. You can do this easily by typing <command> “>” (without quotes) followed by a filename after the command you’re going to execute.
ipconfig /all > codingsec.txt
command
command1

 Copy the output of a command to clipboard

To copy the output of a command to clipboard instead of saving it to a file, you can type <command> | clip
For example, ipconfig /all | clip
comamnd2

Run multiple commands simultaneously

Did you know you could execute multiple commands simultaneously? No? In that case, you have been missing a great functionality of the Command Prompt. You can run multiple commands at the same time using “&&” (without quotes) between two commands and pressing Enter. The command on the left will be executed first, followed by the next command, and so on.
For example, dir && ipconfig /all
command3
command4

Use Autocomplete while typing the location of directory or file

Did you ever have to the location of a directory like “D:\Projects\Opengl\wincodebits\Rotating cube\Debug” in the Command Prompt? If you did, you definitely know how annoying it is !!! But you need not worry anymore  We have a solution for your problem. You can use the Autocomplete feature of Command Prompt. Here’s how to do this :
Say you’re in the drive D: and you need to navigate to D:\Projects\Opengl\wincodebits\Rotating cube\Debug.
Normally, you’d type this : cd Projects\Opengl\wincodebits\Rotating cube\Debug
However, you can just type cd and the rest can be navigated using the TAB key. Try this – type cd and then press the TAB key until the name of the desired directory shows up next to cd. Then type \ and again keep pressing TAB until the desired directory comes up. Keep doing this, until you have reached your destination and press Enter.
command5

View a navigable list of all the commands recently used by you

The Command Prompt stores a list of all the commands used by you recently i.e. in the current cmd session. You can view a navigable and selectable list of recently used commands i.e. you can navigate up and down in that list and select one of those commands. You can do this by pressing the F7 key.
command6
Now, you can select text using the left-button of the mouse and press the Enter key and paste it anywhere in the cmd window by clicking the right mouse button.
If you don’t want to enable QuickEdit, you can still copy/paste text. However, the process would be a little inconvenient. You’ll have to right-click inside the cmd window and select Mark from the pop-up menu. Then, select the text and press Enter. Now, right-click again and select Paste from the pop-up menu.

Run cmd as admin without using the mouse

There are several commands that require the Administrator’s privilege. For running these commands, you need to run cmd as administrator. However, right-clicking the cmd.exe file and selecting Run as Administrator from the list is highly inconvenient. We can help you make cmd run with Administrator’s privileges everytime without using the mouse. There are two ways to do this:
a) Type cmd in the Start Menu’s search box and press Ctrl + Shift + Enter
b) Create a shortcut of cmd. Right click the shortcut file and click on Properties. Under the Shortcut tab, click on the button named Advanced. A new dialog box appears. Check the checkbox labelled Run as Administrator and Click OK. Click OK again to close the Properties Window of the shortcut. Now, whenever you want to open a command prompt window with administrator’s privileges, you’ll have to only double-click this shortcut.

Use function keys in the Command Prompt

The function keys play a significant role in Windows. For example, you know that F1 shows Windows Help, F2is used to rename files and folders, F3 is used to search for files and folders and so on. But did you know that these function keys play a very useful role in the Command Prompt as well? Perhaps not. Here, we’ve listed the function keys that work in the Command Prompt as well as the task they perform :
a) F1 – It pastes the command that was last executed by the Command Prompt, one character at a time.
For example, if the last command was dir, you’ll have to press F1 three times because dir has three characters.
b) F2 – It pastes the command that was last executed by the Command Prompt, however, it asks you to enter a character upto which you want to paste the command.
For example, if the last command was ipconfig /displaydns and you press d after pressing F2, the Command prompt will paste ipconfig / i.e. it will paste all characters before d.
c) F3 – It pastes the last executed command (the whole command).
For example, if the last command was ipconfig /displaydns and you press F3, the Command Prompt will paste ipconfig /displaydns at the location of the cursor.
d) F4 – It displays a dialog which asks the user to enter the character to delete up to.
Note :- When I pressed F4 in my Command Prompt Window, it showed the dialog box mentioned above, but it did not perform any task, no matter which character I entered.
e) F5 – It pastes the last executed command. When pressed repeatedly, it shows all the commands executed, in the reverse order of execution. Once the oldest command i.e. the command which was the first to be executed in that cmd session has been displayed, pressing the F5 key does nothing.
f) F6 – Pastes ^Z to the command prompt.
g) F7 – It displays a navigable list of previously used commands in the form of a dialog.
h) F8 – It pastes recently executed commands. Please note that it is different from F5 because it keeps cycling the previously used commands.
i) F9 – As mentioned above, the command prompt stores a list of all the executed commands. This list is numbered from 0 to 9. When you press F9, you’re asked to enter the number of the command, which you want to use.
For example, to use the first command, type 0. For using the third command, type 2, and so on.

Drag & Drop a file/folder inside Command Prompt

In above, we mentioned how to use autocomplete in Command Prompt and avoid the messy job of typing the entie location of a file/folder. Here we have something even better. You can just drag a file/folder into the Command Prompt window in order to paste its location in the command prompt window.

Get detailed help for commands whose syntax you don’t know

This is one of those tips you can’t afford to miss. Say, you don’t know what is the syntax of the command diris, or what task it performs. You can easily know all about dir by typing help dir. Similarly, most commands would show their information if you type help <command>.

Wednesday, 16 November 2016

PostgreSql Installation document Step by Step

Postgres Server Installation Document
This document discusses detail installation process of PostgreSQL on Linux.PostgreSQL is available integrated with the package management on most Linux platforms. When available, this is the recommended way to install PostgreSQL, since it provides proper integration with the operating system, including automatic patching and other management functionality.




By default, Postgres uses a concept called "roles" to aid in authentication and authorization. These are, in some ways, similar to regular Unix-style accounts, but Postgres does not distinguish between users and groups and instead prefers the more flexible term "role".
Upon installation Postgres is set up to use "ident" authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account. If a Postgres role exists, it can be signed in by logging into the associated Linux system account.
The installation procedure created a user account called postgres that is associated with the default Postgres role.
1-Copy the tar file postgres.tar and extract it to Master & Slave Server by root user .

$tar -xvf postgres.tar

$chown postgres:postgres postgres

2- SSH Authentication
2-(i)-In Master Server:-
ssh-keygen -t rsa
ssh-keygen -t dsa
$cd /root/.ssh
$ cat *.pub >>authorized_keys.node1
2(ii)-In Slave server:-
$ ssh-keygen -t rsa
$ ssh-keygen -t dsa
$ cd /root/.ssh
$ cat *.pub >>authorized_keys.node2
3-
3(i)-In Master server:-
$ cd /root/.ssh
$ scp authorized_keys.node1 root@Slave_IP:/root/.ssh/
3(ii)-In Slave server:-
$ cd /root/.ssh
$ scp authorized_keys.node2 root@Master_IP:/root/.ssh/

4-In Master server and Slave server :-
cat authorized_keys.node* >> authorized_keys
sudo chmod 600 /root/.ssh/authorized_keys

5-Both Master and Slave
$ mkdir –p /var/lib/pgsql
$ groupadd -r -g 26 postgres
$ useradd -r -u 26 -M -d /var/lib/pgsql -n -g postgres postgres
$ passwd postgres

6-Both Master and Slave by root user
scp -r /root/.ssh Master_IP:/var/lib/pgsql // run in slave
scp -r /root/.ssh Slave_IP:/var/lib/pgsql // run in Master
chown -R postgres:postgres /var/lib/pgsql
restorecon -R /var/lib/pgsql/Both Master and Slave by root user

7-run the following rpm for Installation
$cd /postgres
$ rpm -ivh postgresql95-libs-9.5.3-2PGDG.rhel7.x86_64.rpm
$ rpm -ivh postgresql95-9.5.3-2PGDG.rhel7.x86_64.rpm
$ rpm -ivh postgresql95-server-9.5.3-2PGDG.rhel7.x86_64.rpm
$ rpm -ivh postgresql95-contrib-9.5.3-2PGDG.rhel7.x86_64.rpm
$ rpm -ivh repmgr95-3.1.3-1.rhel7.x86_64.rpm

8-Initilize the postgresql by given command
/usr/pgsql-9.5/bin/postgresql95-setup initdb

9-Master Server Replication
Login to Master server
$ cd /var/lib/pgsql/9.5/data
$ mkdir -p archive
$ chown -R postgres:postgres archive

10-Edit the pg_hba.conf file & add the following entry
host repmgr repmgr 10.32.12.131/24 trust
host replication repmgr 10.32.12.131/24 trust
host repmgr repmgr 10.32.12.88/24 trust
host replication repmgr 10.32.12.88/24 trust
Note: Add all slave server ip

11-Edit the postgresql.conf file & add the following entry
listen_addresses = '*'
wal_level = hot_standby
archive_mode = on
archive_command = 'test ! -f /var/lib/pgsql/9.5/data/archive/%f && cp %p /var/lib/pgsql/9.5/data/archive/%f'
max_wal_senders = 2 (Note: this parameter depends on the numbers of server machines , if total nos of server machine is 5 then parameter value will be 5)
wal_keep_segments = 5000
hot_standby = on
wal_log_hints = on
max_replication_slots = 5
shared_preload_libraries = 'repmgr_funcs'

12-Execute commands in Master
$ cd /var/lib/pgsql
$ su - postgres -c "mkdir -p /var/lib/pgsql/repmgr/"
$ vi repmgr.conf
Add the following entry
cluster=db_cluster
node=1
node_name=aglinux54 (Note: Master server's hostname)
conninfo='host=10.32.12.131 user=repmgr dbname=repmgr' (Note: Master server's IP)
pg_bindir=/usr/pgsql-9.5/bin/master_response_timeout=5
reconnect_attempts=2
reconnect_interval=2
failover=automatic
promote_command='/usr/pgsql-9.5/bin/repmgr standby promote -f /var/lib/pgsql/repmgr/repmgr.conf'
follow_command='/usr/pgsql-9.5/bin/repmgr standby follow -f /var/lib/pgsql/repmgr/repmgr.conf'
Change owner

13-Change the permission to postgres
$ chown postgres:postgres /var/lib/pgsql/repmgr/repmgr.conf

14-check the status of the firewalld by using given command
systemctl status firewalld // If enable then stop it.

15-Enable and Start PostgreSQL on the master by root user:
systemctl enable postgresql-9.5
systemctl start postgresql-9.5

16-Create the required users for replication and repmgr and the repmgr DB
$ sudo su – postgres
psql
CREATE ROLE pgpool SUPERUSER CREATEDB CREATEROLE INHERIT REPLICATION LOGIN
ENCRYPTED PASSWORD 'secret';
CREATE USER repmgr SUPERUSER LOGIN ENCRYPTED PASSWORD 'secret';
CREATE DATABASE repmgr OWNER repmgr;
\q
psql -U repmgr -h 10.32.12.131 < /usr/pgsql-9.5/share/contrib/repmgr.sql
psql -U repmgr -h 10.32.12.131 < /usr/pgsql-9.5/share/contrib/repmgr_funcs.sql

17-The below step is to register 10.32.12.131 as master node for repmgr:
$ sudo su - postgres
$ /usr/pgsql-9.5/bin/repmgr -f /var/lib/pgsql/repmgr/repmgr.conf master register

18-When all packages are installed, we can sync the configuration and contents of the primary
with the standby:(Note: Repeat these steps for all Slave server) .

19-login to Slave Server
$ su - postgres
$ /usr/pgsql-9.5/bin/pg_ctl -D /var/lib/pgsql/9.5/data/ -l logfile status --->check the status if running
then stop using below command
$ /usr/pgsql-9.5/bin/pg_ctl -D /var/lib/pgsql/9.5/data/ -l logfile stop

20-To clone data from Master to Slave
$ rm -rf /var/lib/pgsql/9.5/data/*
$ /usr/pgsql-9.5/bin/repmgr -D /var/lib/pgsql/9.5/data -d repmgr -p 5432 -U repmgr -R postgres standby clone 10.32.12.131
$ su - postgres -c "mkdir -p /var/lib/pgsql/repmgr/"
$ cd /var/lib/pgsql/repmgr/
$ vi repmgr.conf
Add the following entry
cluster=db_cluster
node=2 (Note: this parameter depends on the numbers of server machines , if total nos of server
machine is 5then parameter value will be 5)
node_name=aglinux49_3 (Note: put the Slvae server's hostname in this parameter)
conninfo='host=10.32.12.88 user=repmgr dbname=repmgr' (Note: pass the Slave server's ip)
pg_bindir=/usr/pgsql-9.5/bin/
master_response_timeout=5
reconnect_attempts=2
reconnect_interval=2
failover=automatic
promote_command='/usr/pgsql-9.5/bin/repmgr standby promote -f /var/lib/pgsql/repmgr/repmgr.conf'
follow_command='/usr/pgsql-9.5/bin/repmgr standby follow -f /var/lib/pgsql/repmgr/repmgr.conf'
21- Change permission to postgres
$ chown postgres:postgres /var/lib/pgsql/repmgr/repmgr.conf

22- check the status of the firewalld by using given command (Note: by root user)
systemctl status firewalld // If enable then stop it.

23-Enable and Start PostgreSQL on the standby: (Note: by root user)
systemctl enable postgresql-9.5
systemctl start postgresql-9.5

24-Register the Slave servers in repmgr:
$ su - postgres
$ /usr/pgsql-9.5/bin/repmgr -f /var/lib/pgsql/repmgr/repmgr.conf standby register

25- Test the replication on all Slave servers
$ /usr/pgsql-9.5/bin/repmgr -f /var/lib/pgsql/repmgr/repmgr.conf cluster show

26-Before we create the database on the primary, let’s list the databases from the standby-node:
on Slave Servers
$ psql -U postgres -c "\list"
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
repmgr | repmgr | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
Create the database on the primary:
$ psql -U postgres -c "CREATE DATABASE test"

27-Login to the Slave Servers & check
$ psql -U postgres -c "\list"
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
repmgr | repmgr | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| | | | | postgres=CTc/postgres
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(5 rows)

28-To check the role of the Server either Master or Slave
$ /usr/pgsql-9.5/bin/repmgr -f /var/lib/pgsql/repmgr/repmgr.conf cluster show

29-To Start the Daemon
To start daemon on Master server
/usr/pgsql-9.5/bin/repmgrd -d -f /var/lib/pgsql/repmgr/repmgr.conf --verbose –monitoring-history >>/tmp/repsetup.log 2>&1

Connect to Postgres with the New User

Let's assume that you have a Linux system account called test1 (you can create one by typing: adduser test1), and that you have created a Postgres role and database also called test1.
sudo
-i -u test1

You can then connect to the test1 database as the test1 Postgres role by typing:
psql

Configure and initializing PostgreSQL Server

$ sudo /etc/init.d/postgresql-9x start


$ sudo /etc/init.d/postgresql-9x start PostgreSQL 9.x Configuration
---------------------------------
This will initialize PostgreSQL database cluster. The following
questions will determine the database server port, superuser password and
to start server on system boot.Press to accept defaults.
Specify PostgreSQL server port [5432]:
Specify superuser password [password]:
Do you want PostgreSQL server to be started on boot (y/n) [y]:
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale en_US.UTF-8.
The default text search configuration will be set to "english".
fixing permissions on existing directory /opt/postgresql/pg9x/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok c
reating template1 database in /opt/postgresql/pg9x/data/base/1 ... ok
initializing pg_authid ... ok
setting password ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
Success. You can now start the database server using:
/opt/postgresql/pg9x/bin/postgres -D /opt/postgresql/pg9x/data
or
/opt/postgresql/pg9x/bin/pg_ctl -D /opt/postgresql/pg9x/data -l logfile start
Configuring PostgreSQL server startup scripts
Starting PostgreSQL 9.x:
waiting for server to start.... done
server started
PostgreSQL 9.x started successfully
To load this postgres into your environment, source the env file:
. /opt/postgresql/pg9x/pg9x.env



Default database

The default database created at initialization time is:postgres

Default username

The default superuser created at initialization time is:postgres

Default password

The default password (unless you set it at initialization time) is:password

Change the default password

To change the default password, you first need to login to the database with your current password:
$ /opt/postgresql/pg9x/bin/psql -U postgres -d postgres
Password:
psql (9.x)
Type "help" for help.
postgres=#
Now, change the password for the postgres user:
postgres=# ALTER USER postgres WITH PASSWORD 'newpasswordgoeshere';
ALTER ROLE
postgres=#