http://kevinlee.io/w/index.php?title=Special:Contributions/Kevin&feed=atom&limit=50&target=Kevin&year=&month=KevinWiki - User contributions [en-gb]2024-03-28T20:10:33ZFrom KevinWikiMediaWiki 1.16.0http://kevinlee.io/wiki/Linux_User_ManagementLinux User Management2022-03-13T06:38:13Z<p>Kevin: /* Add User to sudo List */</p>
<hr />
<div>[[Category:Linux]]<br />
== Add a new user on Linux ==<br />
<br />
=== Use THIS!!! ===<br />
Details:<br />
https://www.linode.com/docs/security/securing-your-server<br />
<br />
<pre><br />
$ adduser yourusername <br />
$ usermod -a -G sudo yourusername <br />
</pre><br />
Now, use the new username to log in.<br />
<br />
<br />
=== Essential ===<br />
One way to add a new user on Linux is using ''useradd'' command.<br />
<br />
<pre><br />
$ useradd <username> <br />
</pre><br />
# You probably need to use [[wikipedia:en:Sudo|Sudo]] command.<br />
$ sudo useradd <username> <br />
<br />
<br />
=== With Home Directory ===<br />
However, the user added will have neither the password nor the home directory.<br />
If you want to specify the home directory, you can use -d option. If you also want to create home directory and set it for the user when adding a new user, you can use -m option with -d.<br />
<br />
<pre><br />
$ useradd -d <home directory path> -m <username> <br />
<br />
$ useradd -d /home/newuser -m newuser <br />
</pre><br />
<br />
=== With Specifying Login Shell ===<br />
After using useradd above, you may realise that the new user's login shell is not the one you want him or her to have. So in order to specify the login shell, you can -s option.<br />
<br />
<pre><br />
$ useradd -d <home directory path> -m <username> -s <shell> <br />
<br />
$ useradd -d /home/newuser -m newuser -s /bin/bash <br />
</pre><br />
<br />
=== Set User Password ===<br />
<pre><br />
$ passwd USERNAME <br />
Enter new UNIX password: <br />
Retype new UNIX password: <br />
passwd: password updated successfully<br />
</pre><br />
<br />
To get valid login shell list, check out ''/etc/shells'' file.<br />
<pre><br />
$ cat /etc/shells <br />
# /etc/shells: valid login shells<br />
/bin/csh<br />
/usr/bin/es<br />
/usr/bin/ksh<br />
/bin/ksh<br />
/usr/bin/rc<br />
/usr/bin/tcsh<br />
/bin/tcsh<br />
/usr/bin/esh<br />
/usr/bin/screen<br />
/bin/sh<br />
/bin/dash<br />
/bin/false<br />
/bin/bash<br />
/bin/rbash<br />
</pre><br />
<br />
<br />
<br />
=== <code>useradd</code> Help Result ===<br />
<pre><br />
$ useradd --help <br />
Usage: useradd [options] LOGIN<br />
<br />
Options:<br />
-b, --base-dir BASE_DIR base directory for the new user account<br />
home directory<br />
-c, --comment COMMENT set the GECOS field for the new user account<br />
-d, --home-dir HOME_DIR home directory for the new user account<br />
-D, --defaults print or save modified default useradd<br />
configuration<br />
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE<br />
-f, --inactive INACTIVE set password inactive after expiration<br />
to INACTIVE<br />
-g, --gid GROUP force use GROUP for the new user account<br />
-G, --groups GROUPS list of supplementary groups for the new<br />
user account<br />
-h, --help display this help message and exit<br />
-k, --skel SKEL_DIR specify an alternative skel directory<br />
-K, --key KEY=VALUE overrides /etc/login.defs defaults<br />
-m, --create-home create home directory for the new user<br />
account<br />
-o, --non-unique allow create user with duplicate<br />
(non-unique) UID<br />
-p, --password PASSWORD use encrypted password for the new user<br />
account<br />
-r, --system create a system account<br />
-s, --shell SHELL the login shell for the new user account<br />
-u, --uid UID force use the UID for the new user account<br />
</pre><br />
<br />
<br />
== Modify User ==<br />
=== Change Home Directory ===<br />
<pre><br />
$ sudo usermod -d /userhome/path/ <username> <br />
</pre><br />
<br />
=== <code>usermod</code> Help Result ===<br />
<pre><br />
$ usermod --help <br />
Usage: usermod [options] LOGIN<br />
<br />
Options:<br />
-c, --comment COMMENT new value of the GECOS field<br />
-d, --home HOME_DIR new home directory for the user account<br />
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE<br />
-f, --inactive INACTIVE set password inactive after expiration<br />
to INACTIVE<br />
-g, --gid GROUP force use GROUP as new primary group<br />
-G, --groups GROUPS new list of supplementary GROUPS<br />
-a, --append append the user to the supplemental GROUPS<br />
mentioned by the -G option without removing<br />
him/her from other groups<br />
-h, --help display this help message and exit<br />
-l, --login NEW_LOGIN new value of the login name<br />
-L, --lock lock the user account<br />
-m, --move-home move contents of the home directory to the<br />
new location (use only with -d)<br />
-o, --non-unique allow using duplicate (non-unique) UID<br />
-p, --password PASSWORD use encrypted password for the new password<br />
-s, --shell SHELL new login shell for the user account<br />
-u, --uid UID new UID for the user account<br />
-U, --unlock unlock the user account<br />
</pre><br />
<br />
<br />
== Remove User ==<br />
=== Essential ===<br />
A simple way to remove the existing Linux user is using ''userdel'' command.<br />
<br />
<pre><br />
$ userdel <username> <br />
</pre><br />
# You probably need to use [[wikipedia:en:Sudo|Sudo]] command.<br />
$ sudo userdel <username> <br />
<br />
=== Also Remove Home Directory ===<br />
<br />
<pre><br />
$ userdel -r <username> <br />
$ userdel -r newuser <br />
</pre><br />
<br />
=== <code>userdel</code> Help Result ===<br />
<pre><br />
$ userdel --help <br />
Usage: userdel [options] LOGIN<br />
<br />
Options:<br />
-f, --force force removal of files, even if not owned by user<br />
-h, --help display this help message and exit<br />
-r, --remove remove home directory and mail spool<br />
</pre><br />
<br />
<br />
== Sudoer ==<br />
=== Add User to <code>sudo</code> List ===<br />
-To add a uer to sudo list, run the command below<br />
<br />
<pre><br />
$visudo <br />
</pre><br />
<br />
<br />
-Add the '''username''' after <code>root</code><br />
<br />
# User privilege specification<br />
root,'''username''' ALL=(ALL) ALL<br />
<code>i -> edit -> ESC -> : -> wq</code><br />
<br />
<br />
***<br />
If <code>sudo -s</code> in new Ubuntu doesn't add user's <code>HOME</code> to the environment, add the following line to <code>/etc/sudoers</code>.<br />
<pre><br />
Defaults env_keep += "HOME"<br />
</pre><br />
<div style="background: #ffd; border: 1px solid"><br />
<b>WARNING</b>: This can be dangerous so please read https://askubuntu.com/a/1187000 first.<br />
</div><br />
<br />
=== Disable root User ID ===<br />
<div style="background: #ffd; border: 1px solid"><br />
* '''Do not forget to have at least one sudoer user before running this.'''<br />
</div><br />
<pre><br />
$ passwd -l root <br />
</pre><br />
<br />
=== <code>passwd</code> Help Result ===<br />
<pre><br />
$ passwd --help <br />
Usage: passwd [options] [LOGIN]<br />
<br />
Options:<br />
-a, --all report password status on all accounts<br />
-d, --delete delete the password for the named account<br />
-e, --expire force expire the password for the named account<br />
-h, --help display this help message and exit<br />
-k, --keep-tokens change password only if expired<br />
-i, --inactive INACTIVE set password inactive after expiration<br />
to INACTIVE<br />
-l, --lock lock the named account<br />
-n, --mindays MIN_DAYS set minimum number of days before password<br />
change to MIN_DAYS<br />
-q, --quiet quiet mode<br />
-r, --repository REPOSITORY change password in REPOSITORY repository<br />
-S, --status report password status on the named account<br />
-u, --unlock unlock the named account<br />
-w, --warndays WARN_DAYS set expiration warning days to WARN_DAYS<br />
-x, --maxdays MAX_DAYS set maximim number of days before password<br />
change to MAX_DAYS<br />
</pre><br />
<br />
<br />
=== Root Login ===<br />
If you need a persistent root login, use<br />
<pre><br />
$ sudo -i <br />
</pre><br />
<br />
=== References ===<br />
[[https://help.ubuntu.com/community/RootSudo RootSudo]]<br />
<br />
<br />
==Kick Out User Logged in==<br />
===How to===<br />
-Check the processes of the user whom you want to kick out, then kill the user's login shell (e.g. bash)<br />
<br />
-To display all the processes belong to the user.<br />
<pre><br />
$ ps aux | grep ${USER} <br />
</pre><br />
<br />
-To display only PIDs and processes belong to the user<br />
<pre><br />
$ ps aux | grep ${USER} | awk '{print $2 " " $11}' | sort -gr <br />
</pre><br />
<br />
-To kill the process<br />
<pre><br />
$ sudo kill -s 9 <PID number> <br />
</pre><br />
<br />
<br />
=== Example ===<br />
<pre><br />
# username is 'user'<br />
<br />
$ ps aux | grep ${USER} <br />
root 19087 0.0 0.7 67964 2904 ? Ss 13:32 0:00 sshd: user [priv]<br />
user 19089 0.0 0.4 67964 1724 ? S 13:32 0:00 sshd: user@pts/0<br />
user 19090 0.0 0.6 19336 2248 pts/0 Ss 13:32 0:00 -bash<br />
user 19100 0.0 0.2 15056 1088 pts/0 R+ 13:32 0:00 ps aux<br />
user 19101 0.0 0.2 5160 828 pts/0 S+ 13:32 0:00 grep user<br />
</pre><br />
<br />
$ ps aux | grep ${USER} | awk '{print $2 " " $11}' | sort -gr <br />
19105 sort<br />
19104 awk<br />
19103 grep<br />
19102 ps<br />
'''19090 -bash'''<br />
19089 sshd:<br />
19087 sshd:<br />
<br />
-awk option.<br />
<pre><br />
#print the 2nd and the 11th columns<br />
print $2 " " $11<br />
<br />
(sort options<br />
-g, --general-numeric-sort compare according to general numerical value<br />
-r, --reverse reverse the result of comparisons)<br />
</pre><br />
<br />
-Kill the bash<br />
<pre><br />
$ sudo kill -s 9 19090 <br />
</pre><br />
<br />
<br />
===To Kick out Another Login of Yours===<br />
$ who <br />
user pts/0 2008-08-24 13:25 <br />
'''user pts/1 2008-08-24 12:50 ''' <= to kick this out.<br />
<br />
$ ps a <br />
PID TTY STAT TIME COMMAND<br />
2552 tty1 Ss+ 0:00 /sbin/getty 38400 tty1<br />
'''18988 pts/1 Ss+ 0:00 -bash'''<br />
19044 pts/0 Ss 0:00 -bash<br />
19078 pts/0 R+ 0:00 ps a<br />
<br />
$ kill -s 9 18988 <br />
<br />
$ who <br />
user pts/0 2008-08-24 13:25 <br />
<br />
$ ps a <br />
PID TTY STAT TIME COMMAND<br />
2552 tty1 Ss+ 0:00 /sbin/getty 38400 tty1<br />
19044 pts/0 Ss 0:00 -bash<br />
19080 pts/0 R+ 0:00 ps a</div>Kevinhttp://kevinlee.io/wiki/Linux_User_ManagementLinux User Management2022-03-13T06:37:39Z<p>Kevin: /* Add User to sudo List */</p>
<hr />
<div>[[Category:Linux]]<br />
== Add a new user on Linux ==<br />
<br />
=== Use THIS!!! ===<br />
Details:<br />
https://www.linode.com/docs/security/securing-your-server<br />
<br />
<pre><br />
$ adduser yourusername <br />
$ usermod -a -G sudo yourusername <br />
</pre><br />
Now, use the new username to log in.<br />
<br />
<br />
=== Essential ===<br />
One way to add a new user on Linux is using ''useradd'' command.<br />
<br />
<pre><br />
$ useradd <username> <br />
</pre><br />
# You probably need to use [[wikipedia:en:Sudo|Sudo]] command.<br />
$ sudo useradd <username> <br />
<br />
<br />
=== With Home Directory ===<br />
However, the user added will have neither the password nor the home directory.<br />
If you want to specify the home directory, you can use -d option. If you also want to create home directory and set it for the user when adding a new user, you can use -m option with -d.<br />
<br />
<pre><br />
$ useradd -d <home directory path> -m <username> <br />
<br />
$ useradd -d /home/newuser -m newuser <br />
</pre><br />
<br />
=== With Specifying Login Shell ===<br />
After using useradd above, you may realise that the new user's login shell is not the one you want him or her to have. So in order to specify the login shell, you can -s option.<br />
<br />
<pre><br />
$ useradd -d <home directory path> -m <username> -s <shell> <br />
<br />
$ useradd -d /home/newuser -m newuser -s /bin/bash <br />
</pre><br />
<br />
=== Set User Password ===<br />
<pre><br />
$ passwd USERNAME <br />
Enter new UNIX password: <br />
Retype new UNIX password: <br />
passwd: password updated successfully<br />
</pre><br />
<br />
To get valid login shell list, check out ''/etc/shells'' file.<br />
<pre><br />
$ cat /etc/shells <br />
# /etc/shells: valid login shells<br />
/bin/csh<br />
/usr/bin/es<br />
/usr/bin/ksh<br />
/bin/ksh<br />
/usr/bin/rc<br />
/usr/bin/tcsh<br />
/bin/tcsh<br />
/usr/bin/esh<br />
/usr/bin/screen<br />
/bin/sh<br />
/bin/dash<br />
/bin/false<br />
/bin/bash<br />
/bin/rbash<br />
</pre><br />
<br />
<br />
<br />
=== <code>useradd</code> Help Result ===<br />
<pre><br />
$ useradd --help <br />
Usage: useradd [options] LOGIN<br />
<br />
Options:<br />
-b, --base-dir BASE_DIR base directory for the new user account<br />
home directory<br />
-c, --comment COMMENT set the GECOS field for the new user account<br />
-d, --home-dir HOME_DIR home directory for the new user account<br />
-D, --defaults print or save modified default useradd<br />
configuration<br />
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE<br />
-f, --inactive INACTIVE set password inactive after expiration<br />
to INACTIVE<br />
-g, --gid GROUP force use GROUP for the new user account<br />
-G, --groups GROUPS list of supplementary groups for the new<br />
user account<br />
-h, --help display this help message and exit<br />
-k, --skel SKEL_DIR specify an alternative skel directory<br />
-K, --key KEY=VALUE overrides /etc/login.defs defaults<br />
-m, --create-home create home directory for the new user<br />
account<br />
-o, --non-unique allow create user with duplicate<br />
(non-unique) UID<br />
-p, --password PASSWORD use encrypted password for the new user<br />
account<br />
-r, --system create a system account<br />
-s, --shell SHELL the login shell for the new user account<br />
-u, --uid UID force use the UID for the new user account<br />
</pre><br />
<br />
<br />
== Modify User ==<br />
=== Change Home Directory ===<br />
<pre><br />
$ sudo usermod -d /userhome/path/ <username> <br />
</pre><br />
<br />
=== <code>usermod</code> Help Result ===<br />
<pre><br />
$ usermod --help <br />
Usage: usermod [options] LOGIN<br />
<br />
Options:<br />
-c, --comment COMMENT new value of the GECOS field<br />
-d, --home HOME_DIR new home directory for the user account<br />
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE<br />
-f, --inactive INACTIVE set password inactive after expiration<br />
to INACTIVE<br />
-g, --gid GROUP force use GROUP as new primary group<br />
-G, --groups GROUPS new list of supplementary GROUPS<br />
-a, --append append the user to the supplemental GROUPS<br />
mentioned by the -G option without removing<br />
him/her from other groups<br />
-h, --help display this help message and exit<br />
-l, --login NEW_LOGIN new value of the login name<br />
-L, --lock lock the user account<br />
-m, --move-home move contents of the home directory to the<br />
new location (use only with -d)<br />
-o, --non-unique allow using duplicate (non-unique) UID<br />
-p, --password PASSWORD use encrypted password for the new password<br />
-s, --shell SHELL new login shell for the user account<br />
-u, --uid UID new UID for the user account<br />
-U, --unlock unlock the user account<br />
</pre><br />
<br />
<br />
== Remove User ==<br />
=== Essential ===<br />
A simple way to remove the existing Linux user is using ''userdel'' command.<br />
<br />
<pre><br />
$ userdel <username> <br />
</pre><br />
# You probably need to use [[wikipedia:en:Sudo|Sudo]] command.<br />
$ sudo userdel <username> <br />
<br />
=== Also Remove Home Directory ===<br />
<br />
<pre><br />
$ userdel -r <username> <br />
$ userdel -r newuser <br />
</pre><br />
<br />
=== <code>userdel</code> Help Result ===<br />
<pre><br />
$ userdel --help <br />
Usage: userdel [options] LOGIN<br />
<br />
Options:<br />
-f, --force force removal of files, even if not owned by user<br />
-h, --help display this help message and exit<br />
-r, --remove remove home directory and mail spool<br />
</pre><br />
<br />
<br />
== Sudoer ==<br />
=== Add User to <code>sudo</code> List ===<br />
-To add a uer to sudo list, run the command below<br />
<br />
<pre><br />
$visudo <br />
</pre><br />
<br />
<br />
-Add the '''username''' after <code>root</code><br />
<br />
# User privilege specification<br />
root,'''username''' ALL=(ALL) ALL<br />
<code>i -> edit -> ESC -> : -> wq</code><br />
<br />
<br />
***<br />
If <code>sudo -s</code> in new Ubuntu doesn't add user's <code>HOME</code> to the environment, add the following line to <code>/etc/sudoers</code>.<br />
<pre><br />
Defaults env_keep += "HOME"<br />
</pre><br />
<b>WARNING</b>: This can be dangerous so please read https://askubuntu.com/a/1187000 first.<br />
<br />
=== Disable root User ID ===<br />
<div style="background: #ffd; border: 1px solid"><br />
* '''Do not forget to have at least one sudoer user before running this.'''<br />
</div><br />
<pre><br />
$ passwd -l root <br />
</pre><br />
<br />
=== <code>passwd</code> Help Result ===<br />
<pre><br />
$ passwd --help <br />
Usage: passwd [options] [LOGIN]<br />
<br />
Options:<br />
-a, --all report password status on all accounts<br />
-d, --delete delete the password for the named account<br />
-e, --expire force expire the password for the named account<br />
-h, --help display this help message and exit<br />
-k, --keep-tokens change password only if expired<br />
-i, --inactive INACTIVE set password inactive after expiration<br />
to INACTIVE<br />
-l, --lock lock the named account<br />
-n, --mindays MIN_DAYS set minimum number of days before password<br />
change to MIN_DAYS<br />
-q, --quiet quiet mode<br />
-r, --repository REPOSITORY change password in REPOSITORY repository<br />
-S, --status report password status on the named account<br />
-u, --unlock unlock the named account<br />
-w, --warndays WARN_DAYS set expiration warning days to WARN_DAYS<br />
-x, --maxdays MAX_DAYS set maximim number of days before password<br />
change to MAX_DAYS<br />
</pre><br />
<br />
<br />
=== Root Login ===<br />
If you need a persistent root login, use<br />
<pre><br />
$ sudo -i <br />
</pre><br />
<br />
=== References ===<br />
[[https://help.ubuntu.com/community/RootSudo RootSudo]]<br />
<br />
<br />
==Kick Out User Logged in==<br />
===How to===<br />
-Check the processes of the user whom you want to kick out, then kill the user's login shell (e.g. bash)<br />
<br />
-To display all the processes belong to the user.<br />
<pre><br />
$ ps aux | grep ${USER} <br />
</pre><br />
<br />
-To display only PIDs and processes belong to the user<br />
<pre><br />
$ ps aux | grep ${USER} | awk '{print $2 " " $11}' | sort -gr <br />
</pre><br />
<br />
-To kill the process<br />
<pre><br />
$ sudo kill -s 9 <PID number> <br />
</pre><br />
<br />
<br />
=== Example ===<br />
<pre><br />
# username is 'user'<br />
<br />
$ ps aux | grep ${USER} <br />
root 19087 0.0 0.7 67964 2904 ? Ss 13:32 0:00 sshd: user [priv]<br />
user 19089 0.0 0.4 67964 1724 ? S 13:32 0:00 sshd: user@pts/0<br />
user 19090 0.0 0.6 19336 2248 pts/0 Ss 13:32 0:00 -bash<br />
user 19100 0.0 0.2 15056 1088 pts/0 R+ 13:32 0:00 ps aux<br />
user 19101 0.0 0.2 5160 828 pts/0 S+ 13:32 0:00 grep user<br />
</pre><br />
<br />
$ ps aux | grep ${USER} | awk '{print $2 " " $11}' | sort -gr <br />
19105 sort<br />
19104 awk<br />
19103 grep<br />
19102 ps<br />
'''19090 -bash'''<br />
19089 sshd:<br />
19087 sshd:<br />
<br />
-awk option.<br />
<pre><br />
#print the 2nd and the 11th columns<br />
print $2 " " $11<br />
<br />
(sort options<br />
-g, --general-numeric-sort compare according to general numerical value<br />
-r, --reverse reverse the result of comparisons)<br />
</pre><br />
<br />
-Kill the bash<br />
<pre><br />
$ sudo kill -s 9 19090 <br />
</pre><br />
<br />
<br />
===To Kick out Another Login of Yours===<br />
$ who <br />
user pts/0 2008-08-24 13:25 <br />
'''user pts/1 2008-08-24 12:50 ''' <= to kick this out.<br />
<br />
$ ps a <br />
PID TTY STAT TIME COMMAND<br />
2552 tty1 Ss+ 0:00 /sbin/getty 38400 tty1<br />
'''18988 pts/1 Ss+ 0:00 -bash'''<br />
19044 pts/0 Ss 0:00 -bash<br />
19078 pts/0 R+ 0:00 ps a<br />
<br />
$ kill -s 9 18988 <br />
<br />
$ who <br />
user pts/0 2008-08-24 13:25 <br />
<br />
$ ps a <br />
PID TTY STAT TIME COMMAND<br />
2552 tty1 Ss+ 0:00 /sbin/getty 38400 tty1<br />
19044 pts/0 Ss 0:00 -bash<br />
19080 pts/0 R+ 0:00 ps a</div>Kevinhttp://kevinlee.io/wiki/PostgreSQLPostgreSQL2016-01-02T01:00:19Z<p>Kevin: /* Restore Database */</p>
<hr />
<div>[[Category:Linux]]<br />
[[Category:Software Development]]<br />
[[Category:Database]]<br />
{{Infobox software<br />
| name = PostgreSQL<br />
| logo = <br />
| screenshot =<br />
| caption =<br />
| developer = PostgreSQL Global Development Group<br />
| latest release version = 9.2.2,<br />9.1.7,<br />9.0.11,<br />8.4.15,<br />8.3.22<br />
| latest release date = <br />{{Start date and age|2012|12|06}}<br />
| latest preview version = <!--Add alpha, when it becomes available--><br />
| latest preview date = <!--{{Start date and age|YYYY|MM|DD}}--><br />
| operating system = [[Cross-platform]]<br />
| programming language = [[C (programming language)|C]]<br />
| genre = [[Object-relational database|ORDBMS]]<br />
| license = PostgreSQL licence<ref>{{cite web |date=2010-02-18|title=PostgreSQL licence approved by OSI| url= http://www.crynwr.com/cgi-bin/ezmlm-cgi?17:mmp:969| publisher = Crynwr | accessdate = 2010-02-18}}</ref><ref>{{cite web | publisher = Open Source Initiative | date = 2010-02-20|title=OSI PostgreSQL licence|url=http://www.opensource.org/licenses/postgresql | accessdate = 2010-02-20}}</ref><ref>{{cite web|url= http://www.postgresql.org/about/licence|title= License| publisher = PostgreSQL Global Development Group|accessdate=2010-09-20}}</ref><br />
| website = {{URL|www.postgresql.org}}<br />
}}<br />
<br />
==PostgreSQL==<br />
[http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] is an object-relational database management system (ORDBMS). The documentation of PostgreSQL 8.4.4 can be found [http://www.postgresql.org/docs/8.4/static here]<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static | title = PostgreSQL 8.4.4 Documentation | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
==Installation==<br />
*Install PostgreSQL<br />
<pre><br />
$ sudo apt-get install postgresql <br />
</pre><br />
<br />
=== For Latest Version ===<br />
<pre><br />
vim /etc/apt/sources.list.d/pgdg.list <br />
</pre><br />
<br />
add<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ YOUR_UBUNTU_VERSION_HERE-pgdg main <br />
</pre><br />
e.g.) 14.04<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main <br />
</pre><br />
<br />
Then import the repository signing key.<br />
<pre><br />
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \<br />
sudo apt-key add - <br />
</pre><br />
<br />
Now update and install.<br />
<pre><br />
apt-get update <br />
apt-get install postgresql <br />
</pre><br />
<br />
* Reference: http://www.postgresql.org/download/linux/ubuntu/<br />
<br />
==Administration==<br />
=== Setup ===<br />
====Setup Authentication Methods====<br />
* In order to allow new users to log in the PostgreSQL server, <code>/etc/postgresql/8.4/main/pg_hba.conf</code> should be modified.<br />
<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all ident<br />
# IPv4 local connections:<br />
host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
host all all ::1/128 md5<br />
To<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all md5<br />
# IPv4 local connections:<br />
#host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
#host all all ::1/128 md5<br />
Allowing the local access only with username & password.<br />
<br />
<br />
==== Access Remotely ====<br />
* Edit <code>/etc/postgresql/9.1/main/postgresql.conf</code><br />
<pre><br />
listen_addresses = 'localhost,address,separated,by,comma'<br />
</pre><br />
'''The IP is the local one but NOT a remote one'''.<br />
e.g.) 192.168.0.111 means it listens any access to 192.168.0.111 (local IP).<br />
<br />
e.g.)<br />
<pre><br />
listen_addresses = 'localhost,192.168.0.111'<br />
</pre><br />
<br />
* Edit <code>/etc/postgresql/9.1/main/pg_hba.conf</code><br />
<pre><br />
host all all IP/HERE trust<br />
</pre><br />
e.g.)<br />
<pre><br />
host all all 192.168.0.121/32 trust<br />
</pre><br />
'''The IP is not the local one but a remote one'''.<br />
e.g.) 192.168.0.121 means access from 192.168.0.121 is allowed.<br />
<br />
* Test if it works.<br />
<pre><br />
# -W: force password prompt<br />
$ psql -h IP-TO-DB-SERVER -U USERNAME -W <br />
</pre><br />
e.g.)<br />
<pre><br />
$ psql -h 192.168.0.123 -U db_user -W <br />
</pre><br />
<br />
===Roles (Users)===<br />
====Setup Root User Password====<br />
*Set up the password of the PostgreSQL root user<br />
$ sudo -u postgres psql postgres <br />
<br />
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.<br />
<br />
Type: \copyright for distribution terms<br />
\h for help with SQL commands<br />
\? for help with psql commands<br />
\g or terminate with semicolon to execute query<br />
\q to quit<br />
<br />
postgres=# '''ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';''' <br />
or<br />
postgres=# '''\password postgres''' <br />
<br />
Use <code>\quit</code> or <code>\q</code> to quit psql.<br />
postgres=# \q <br />
<br />
<br />
====Create Role (User)====<br />
Creating a user in PostgreSQL is in fact creating a role. So there are two ways to do it. The first one is using <code>CREATE ROLE</code><ref>{{cite web |url=http://www.postgresql.org/docs/8.4/static/database-roles.html | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles | date = 2009 | accessdate = 2010-08-02}}</ref> SQL command and the other one is using the programme, <code>createuser</code><ref>{{cite web|url= http://www.postgresql.org/docs/8.4/static/app-createuser.html | title= PostgreSQL 8.4.4 Documentation - createuser | date = 2009 | accessdate = 2010-08-02}}</ref>.<br />
<br />
=====CREATE ROLE=====<br />
<code>CREATE ROLE</code><ref>{{cite web |url = http://www.postgresql.org/docs/8.4/static/sql-createrole.html | title = PostgreSQL 8.4.4 Documentation - CREATE ROLE | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
CREATE ROLE role_name; <br />
CREATE ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
CREATE USER role_name WITH ENCRYPTED PASSWORD 'password'; <br />
(CREATE USER == CREATE ROLE + LOGIN) <br />
<br />
'''Usually use this.'''<br />
CREATE ROLE "role_name" WITH LOGIN ENCRYPTED PASSWORD 'password';<br />
<br />
=====createuser=====<br />
createuser [option...] [username] <br />
<br />
So to create a new user and a db belonging to him/her.<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-P: it will ask you to enter the password for the new user ('''role''').<br />
<br />
<br />
* Options<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createuser.html#AEN67555 | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles#Options | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
createuser accepts the following command-line arguments:<br />
<br />
username<br />
<br />
Specifies the name of the PostgreSQL user to be created. This name must be different from all existing roles in this PostgreSQL installation. <br />
-s<br />
--superuser<br />
<br />
The new user will be a superuser. <br />
-S<br />
--no-superuser<br />
<br />
The new user will not be a superuser. This is the default. <br />
-d<br />
--createdb<br />
<br />
The new user will be allowed to create databases. <br />
-D<br />
--no-createdb<br />
<br />
The new user will not be allowed to create databases. This is the default. <br />
-r<br />
--createrole<br />
<br />
The new user will be allowed to create new roles (that is, this user will have CREATEROLE privilege). <br />
-R<br />
--no-createrole<br />
<br />
The new user will not be allowed to create new roles. This is the default. <br />
-l<br />
--login<br />
<br />
The new user will be allowed to log in (that is, the user name can be used as the initial session user identifier). This is the default. <br />
-L<br />
--no-login<br />
<br />
The new user will not be allowed to log in. (A role without login privilege is still useful as a means of managing database permissions.) <br />
-i<br />
--inherit<br />
<br />
The new role will automatically inherit privileges of roles it is a member of. This is the default. <br />
-I<br />
--no-inherit<br />
<br />
The new role will not automatically inherit privileges of roles it is a member of. <br />
-c number<br />
--connection-limit number<br />
<br />
Set a maximum number of connections for the new user. The default is to set no limit. <br />
-P<br />
--pwprompt<br />
<br />
If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication. <br />
-E<br />
--encrypted<br />
<br />
Encrypts the user's password stored in the database. If not specified, the default password behavior is used. <br />
-N<br />
--unencrypted<br />
<br />
Does not encrypt the user's password stored in the database. If not specified, the default password behavior is used. <br />
-e<br />
--echo<br />
<br />
Echo the commands that createuser generates and sends to the server. <br />
<br />
You will be prompted for a name and other missing information if it is not specified on the command line.<br />
<br />
createuser also accepts the following command-line arguments for connection parameters:<br />
<br />
-h host<br />
--host host<br />
<br />
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. <br />
-p port<br />
--port port<br />
<br />
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. <br />
-U username<br />
--username username<br />
<br />
User name to connect as (not the user name to create). <br />
-w<br />
--no-password<br />
<br />
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. <br />
-W<br />
--password<br />
<br />
Force createuser to prompt for a password (for connecting to the server, not for the password of the new user).<br />
<br />
This option is never essential, since createuser will automatically prompt for a password if the server demands password authentication. However, createuser will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt. <br />
<br />
<br />
====View Roles====<br />
<br />
* To see all the exising roles.<br />
SELECT * FROM pg_roles;<br />
<br />
* To see the details of an existing role<br />
SELECT role_name FROM pg_roles; <br />
<br />
<br />
====Change Role====<br />
* <code>ALTER ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-alterrole.html | title = PostgreSQL 8.4.4 Documentation - ALTER ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
ALTER ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
<br />
<br />
====Remove Role====<br />
* Using <code>DROP ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-droprole.html | title = PostgreSQL 8.4.4 Documentation - DROP ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
DROP ROLE role_name; <br />
<br />
*Using <code>dropuser</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-dropuser.html | title = PostgreSQL 8.4.4 Documentation - dropuser | date = 2009 | accessdate = 2010-08-02}}</ref><br />
dropuser name <br />
<br />
===Databases===<br />
====List Databases====<br />
postgres=# \l <br />
<br />
====Select (Change) Database====<br />
postgres=# \c DB_NAME <br />
<br />
====List Table====<br />
<br />
postgres=# \d <br />
<br />
====Display Table Description====<br />
<br />
postgres=# \d TABLE_NAME <br />
<br />
<br />
====Creating Database====<br />
* Using <code>CREATE DATABASE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-createdatabase.html | title = PostgreSQL 8.4.4 Documentation - CREATE DATABASE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL to create a new database.<br />
CREATE DATABASE "db_name" <br />
<br />
CREATE DATABASE "db_name" OWNER "role_name" <br />
<br />
* Similar to creating a role, it is also possible to create a new database from the shell by using <code>createdb</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createdb.html | title = PostgreSQL 8.4.4 Documentation - createdb | date = 2009 | accessdate = 2010-08-02}}</ref> command.<br />
createdb [option...] [db_name] [description]<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-O owner<br />
--owner owner<br />
Specifies the database user who will own the new database.<br />
<br />
====Change Database====<br />
e.g.)<br />
postgres=# ALTER DATABASE db_name OWNER TO new_owner; <br />
<br />
====Privileges====<br />
=====GRANT=====<br />
e.g.)<br />
postgres=# GRANT ALL PRIVILEGES ON DATABASE db_name TO username; <br />
<br />
<br />
====Drop and Recreate database====<br />
* Access db<br />
$ sudo -u postgres psql postgres <br />
And drop and recreate the database.<br />
DROP DATABASE DB_NAME; <br />
CREATE DATABASE DB_NAME OWNER ROLE_NAME; <br />
<br />
====Back Up Database====<br />
$ sudo -u postgres pg_dump DB_NAME > DB_DUMP_DB_NAME.sql<br />
<br />
To custom-format archive file<br />
<pre><br />
pg_dump -Fc DB_NAME > "FILENAME"<br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_dump -Fc my_db > "my_db_backup.dump.compressed" <br />
</pre><br />
<br />
* Backup the entire contents of DB cluster<br />
<pre><br />
pg_dumpall > FILE <br />
</pre><br />
<br />
====Restore Database====<br />
$ sudo -u postgres psql --set ON_ERROR_STOP=on DB_NAME < DB_DUMP_DB_NAME.sql <br />
<br />
<pre><br />
pg_restore --no-owner -U USER -h HOST -d DB_NAME FILENAME <br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_restore --no-owner -U my_db_user -h HOST -d my_db "my_db_backup.dump.compressed" <br />
</pre><br />
<br />
* Restore all databases backed up by pg_dumpall<br />
<pre><br />
psql -f FILE postgres<br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ sudo -u postgres psql --set ON_ERROR_STOP=on -f my_db_dump.sql postgres <br />
</pre><br />
<br />
====Run SQL Script====<br />
* Use psql terminal and select DB.<br />
$ sudo -u postgres psql <br />
postgres=# \c DB_NAME <br />
OR Access the DB directly<br />
$ sudo -u postgres psql DB_NAME <br />
DB_NAME=# <br />
<br />
* Run SQL script from within psql<br />
DB_NAME=# \set ON_ERROR_STOP <br />
DB_NAME=# \i db.sql<br />
<br />
* Run SQL query<br />
DB_NAME=# type SQL; <br />
e.g.)<br />
DB_NAME=# SELECT * FROM some_table;<br />
<br />
====Store the Query Result in File====<br />
DB_NAME=# \o file_name <br />
# Run some query<br />
DB_NAME=# SELECT * FROM some_table; <br />
# \o to finish<br />
DB_NAME=# \o<br />
==== Store Query Result in CSV file ====<br />
<pre><br />
\copy (SELECT * FROM products WHERE product_name LIKE 'SomePreFix%') To '/path/to/store/csv-file.csv' With CSV HEADER; <br />
</pre><br />
<br />
====Terminate any processing user of database====<br />
* To terminate all the processing users of the database, run the following script.<br />
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='DB_NAME';<br />
<br />
===Restart Server===<br />
* To restart the server, /etc/init.d/postgresql-8.x (depending on the version).<br />
sudo /etc/init.d/postgresql-8.4 restart<br />
<br />
==Install pgAdmin III==<br />
<pre><br />
$ sudo apt-get install pgadmin3 <br />
</pre><br />
<br />
==Troubleshooting==<br />
===Shared Memory Segment Issue===<br />
* http://askubuntu.com/questions/44373/how-to-fix-postgresql-installation<br />
* http://leopard.in.ua/2013/09/05/postgresql-sessting-shared-memory<br />
<br />
==References==<br />
{{Reflist|1}}</div>Kevinhttp://kevinlee.io/wiki/PostgreSQLPostgreSQL2016-01-01T16:18:16Z<p>Kevin: /* Restore Database */</p>
<hr />
<div>[[Category:Linux]]<br />
[[Category:Software Development]]<br />
[[Category:Database]]<br />
{{Infobox software<br />
| name = PostgreSQL<br />
| logo = <br />
| screenshot =<br />
| caption =<br />
| developer = PostgreSQL Global Development Group<br />
| latest release version = 9.2.2,<br />9.1.7,<br />9.0.11,<br />8.4.15,<br />8.3.22<br />
| latest release date = <br />{{Start date and age|2012|12|06}}<br />
| latest preview version = <!--Add alpha, when it becomes available--><br />
| latest preview date = <!--{{Start date and age|YYYY|MM|DD}}--><br />
| operating system = [[Cross-platform]]<br />
| programming language = [[C (programming language)|C]]<br />
| genre = [[Object-relational database|ORDBMS]]<br />
| license = PostgreSQL licence<ref>{{cite web |date=2010-02-18|title=PostgreSQL licence approved by OSI| url= http://www.crynwr.com/cgi-bin/ezmlm-cgi?17:mmp:969| publisher = Crynwr | accessdate = 2010-02-18}}</ref><ref>{{cite web | publisher = Open Source Initiative | date = 2010-02-20|title=OSI PostgreSQL licence|url=http://www.opensource.org/licenses/postgresql | accessdate = 2010-02-20}}</ref><ref>{{cite web|url= http://www.postgresql.org/about/licence|title= License| publisher = PostgreSQL Global Development Group|accessdate=2010-09-20}}</ref><br />
| website = {{URL|www.postgresql.org}}<br />
}}<br />
<br />
==PostgreSQL==<br />
[http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] is an object-relational database management system (ORDBMS). The documentation of PostgreSQL 8.4.4 can be found [http://www.postgresql.org/docs/8.4/static here]<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static | title = PostgreSQL 8.4.4 Documentation | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
==Installation==<br />
*Install PostgreSQL<br />
<pre><br />
$ sudo apt-get install postgresql <br />
</pre><br />
<br />
=== For Latest Version ===<br />
<pre><br />
vim /etc/apt/sources.list.d/pgdg.list <br />
</pre><br />
<br />
add<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ YOUR_UBUNTU_VERSION_HERE-pgdg main <br />
</pre><br />
e.g.) 14.04<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main <br />
</pre><br />
<br />
Then import the repository signing key.<br />
<pre><br />
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \<br />
sudo apt-key add - <br />
</pre><br />
<br />
Now update and install.<br />
<pre><br />
apt-get update <br />
apt-get install postgresql <br />
</pre><br />
<br />
* Reference: http://www.postgresql.org/download/linux/ubuntu/<br />
<br />
==Administration==<br />
=== Setup ===<br />
====Setup Authentication Methods====<br />
* In order to allow new users to log in the PostgreSQL server, <code>/etc/postgresql/8.4/main/pg_hba.conf</code> should be modified.<br />
<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all ident<br />
# IPv4 local connections:<br />
host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
host all all ::1/128 md5<br />
To<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all md5<br />
# IPv4 local connections:<br />
#host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
#host all all ::1/128 md5<br />
Allowing the local access only with username & password.<br />
<br />
<br />
==== Access Remotely ====<br />
* Edit <code>/etc/postgresql/9.1/main/postgresql.conf</code><br />
<pre><br />
listen_addresses = 'localhost,address,separated,by,comma'<br />
</pre><br />
'''The IP is the local one but NOT a remote one'''.<br />
e.g.) 192.168.0.111 means it listens any access to 192.168.0.111 (local IP).<br />
<br />
e.g.)<br />
<pre><br />
listen_addresses = 'localhost,192.168.0.111'<br />
</pre><br />
<br />
* Edit <code>/etc/postgresql/9.1/main/pg_hba.conf</code><br />
<pre><br />
host all all IP/HERE trust<br />
</pre><br />
e.g.)<br />
<pre><br />
host all all 192.168.0.121/32 trust<br />
</pre><br />
'''The IP is not the local one but a remote one'''.<br />
e.g.) 192.168.0.121 means access from 192.168.0.121 is allowed.<br />
<br />
* Test if it works.<br />
<pre><br />
# -W: force password prompt<br />
$ psql -h IP-TO-DB-SERVER -U USERNAME -W <br />
</pre><br />
e.g.)<br />
<pre><br />
$ psql -h 192.168.0.123 -U db_user -W <br />
</pre><br />
<br />
===Roles (Users)===<br />
====Setup Root User Password====<br />
*Set up the password of the PostgreSQL root user<br />
$ sudo -u postgres psql postgres <br />
<br />
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.<br />
<br />
Type: \copyright for distribution terms<br />
\h for help with SQL commands<br />
\? for help with psql commands<br />
\g or terminate with semicolon to execute query<br />
\q to quit<br />
<br />
postgres=# '''ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';''' <br />
or<br />
postgres=# '''\password postgres''' <br />
<br />
Use <code>\quit</code> or <code>\q</code> to quit psql.<br />
postgres=# \q <br />
<br />
<br />
====Create Role (User)====<br />
Creating a user in PostgreSQL is in fact creating a role. So there are two ways to do it. The first one is using <code>CREATE ROLE</code><ref>{{cite web |url=http://www.postgresql.org/docs/8.4/static/database-roles.html | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles | date = 2009 | accessdate = 2010-08-02}}</ref> SQL command and the other one is using the programme, <code>createuser</code><ref>{{cite web|url= http://www.postgresql.org/docs/8.4/static/app-createuser.html | title= PostgreSQL 8.4.4 Documentation - createuser | date = 2009 | accessdate = 2010-08-02}}</ref>.<br />
<br />
=====CREATE ROLE=====<br />
<code>CREATE ROLE</code><ref>{{cite web |url = http://www.postgresql.org/docs/8.4/static/sql-createrole.html | title = PostgreSQL 8.4.4 Documentation - CREATE ROLE | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
CREATE ROLE role_name; <br />
CREATE ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
CREATE USER role_name WITH ENCRYPTED PASSWORD 'password'; <br />
(CREATE USER == CREATE ROLE + LOGIN) <br />
<br />
'''Usually use this.'''<br />
CREATE ROLE "role_name" WITH LOGIN ENCRYPTED PASSWORD 'password';<br />
<br />
=====createuser=====<br />
createuser [option...] [username] <br />
<br />
So to create a new user and a db belonging to him/her.<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-P: it will ask you to enter the password for the new user ('''role''').<br />
<br />
<br />
* Options<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createuser.html#AEN67555 | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles#Options | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
createuser accepts the following command-line arguments:<br />
<br />
username<br />
<br />
Specifies the name of the PostgreSQL user to be created. This name must be different from all existing roles in this PostgreSQL installation. <br />
-s<br />
--superuser<br />
<br />
The new user will be a superuser. <br />
-S<br />
--no-superuser<br />
<br />
The new user will not be a superuser. This is the default. <br />
-d<br />
--createdb<br />
<br />
The new user will be allowed to create databases. <br />
-D<br />
--no-createdb<br />
<br />
The new user will not be allowed to create databases. This is the default. <br />
-r<br />
--createrole<br />
<br />
The new user will be allowed to create new roles (that is, this user will have CREATEROLE privilege). <br />
-R<br />
--no-createrole<br />
<br />
The new user will not be allowed to create new roles. This is the default. <br />
-l<br />
--login<br />
<br />
The new user will be allowed to log in (that is, the user name can be used as the initial session user identifier). This is the default. <br />
-L<br />
--no-login<br />
<br />
The new user will not be allowed to log in. (A role without login privilege is still useful as a means of managing database permissions.) <br />
-i<br />
--inherit<br />
<br />
The new role will automatically inherit privileges of roles it is a member of. This is the default. <br />
-I<br />
--no-inherit<br />
<br />
The new role will not automatically inherit privileges of roles it is a member of. <br />
-c number<br />
--connection-limit number<br />
<br />
Set a maximum number of connections for the new user. The default is to set no limit. <br />
-P<br />
--pwprompt<br />
<br />
If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication. <br />
-E<br />
--encrypted<br />
<br />
Encrypts the user's password stored in the database. If not specified, the default password behavior is used. <br />
-N<br />
--unencrypted<br />
<br />
Does not encrypt the user's password stored in the database. If not specified, the default password behavior is used. <br />
-e<br />
--echo<br />
<br />
Echo the commands that createuser generates and sends to the server. <br />
<br />
You will be prompted for a name and other missing information if it is not specified on the command line.<br />
<br />
createuser also accepts the following command-line arguments for connection parameters:<br />
<br />
-h host<br />
--host host<br />
<br />
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. <br />
-p port<br />
--port port<br />
<br />
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. <br />
-U username<br />
--username username<br />
<br />
User name to connect as (not the user name to create). <br />
-w<br />
--no-password<br />
<br />
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. <br />
-W<br />
--password<br />
<br />
Force createuser to prompt for a password (for connecting to the server, not for the password of the new user).<br />
<br />
This option is never essential, since createuser will automatically prompt for a password if the server demands password authentication. However, createuser will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt. <br />
<br />
<br />
====View Roles====<br />
<br />
* To see all the exising roles.<br />
SELECT * FROM pg_roles;<br />
<br />
* To see the details of an existing role<br />
SELECT role_name FROM pg_roles; <br />
<br />
<br />
====Change Role====<br />
* <code>ALTER ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-alterrole.html | title = PostgreSQL 8.4.4 Documentation - ALTER ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
ALTER ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
<br />
<br />
====Remove Role====<br />
* Using <code>DROP ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-droprole.html | title = PostgreSQL 8.4.4 Documentation - DROP ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
DROP ROLE role_name; <br />
<br />
*Using <code>dropuser</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-dropuser.html | title = PostgreSQL 8.4.4 Documentation - dropuser | date = 2009 | accessdate = 2010-08-02}}</ref><br />
dropuser name <br />
<br />
===Databases===<br />
====List Databases====<br />
postgres=# \l <br />
<br />
====Select (Change) Database====<br />
postgres=# \c DB_NAME <br />
<br />
====List Table====<br />
<br />
postgres=# \d <br />
<br />
====Display Table Description====<br />
<br />
postgres=# \d TABLE_NAME <br />
<br />
<br />
====Creating Database====<br />
* Using <code>CREATE DATABASE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-createdatabase.html | title = PostgreSQL 8.4.4 Documentation - CREATE DATABASE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL to create a new database.<br />
CREATE DATABASE "db_name" <br />
<br />
CREATE DATABASE "db_name" OWNER "role_name" <br />
<br />
* Similar to creating a role, it is also possible to create a new database from the shell by using <code>createdb</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createdb.html | title = PostgreSQL 8.4.4 Documentation - createdb | date = 2009 | accessdate = 2010-08-02}}</ref> command.<br />
createdb [option...] [db_name] [description]<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-O owner<br />
--owner owner<br />
Specifies the database user who will own the new database.<br />
<br />
====Change Database====<br />
e.g.)<br />
postgres=# ALTER DATABASE db_name OWNER TO new_owner; <br />
<br />
====Privileges====<br />
=====GRANT=====<br />
e.g.)<br />
postgres=# GRANT ALL PRIVILEGES ON DATABASE db_name TO username; <br />
<br />
<br />
====Drop and Recreate database====<br />
* Access db<br />
$ sudo -u postgres psql postgres <br />
And drop and recreate the database.<br />
DROP DATABASE DB_NAME; <br />
CREATE DATABASE DB_NAME OWNER ROLE_NAME; <br />
<br />
====Back Up Database====<br />
$ sudo -u postgres pg_dump DB_NAME > DB_DUMP_DB_NAME.sql<br />
<br />
To custom-format archive file<br />
<pre><br />
pg_dump -Fc DB_NAME > "FILENAME"<br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_dump -Fc my_db > "my_db_backup.dump.compressed" <br />
</pre><br />
<br />
* Backup the entire contents of DB cluster<br />
<pre><br />
pg_dumpall > FILE <br />
</pre><br />
<br />
====Restore Database====<br />
$ sudo -u postgres psql --set ON_ERROR_STOP=on DB_NAME < DB_DUMP_DB_NAME.sql <br />
<br />
<pre><br />
pg_restore --no-owner -U USER -h HOST -d DB_NAME FILENAME <br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_restore --no-owner -U my_db_user -h HOST -d my_db "my_db_backup.dump.compressed" <br />
</pre><br />
<br />
* Restore all databases backed up by pg_dumpall<br />
<pre><br />
psql -f FILE postgres<br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ sudo -u postgres psql -f my_db_dump.sql postgres <br />
</pre><br />
<br />
====Run SQL Script====<br />
* Use psql terminal and select DB.<br />
$ sudo -u postgres psql <br />
postgres=# \c DB_NAME <br />
OR Access the DB directly<br />
$ sudo -u postgres psql DB_NAME <br />
DB_NAME=# <br />
<br />
* Run SQL script from within psql<br />
DB_NAME=# \set ON_ERROR_STOP <br />
DB_NAME=# \i db.sql<br />
<br />
* Run SQL query<br />
DB_NAME=# type SQL; <br />
e.g.)<br />
DB_NAME=# SELECT * FROM some_table;<br />
<br />
====Store the Query Result in File====<br />
DB_NAME=# \o file_name <br />
# Run some query<br />
DB_NAME=# SELECT * FROM some_table; <br />
# \o to finish<br />
DB_NAME=# \o<br />
==== Store Query Result in CSV file ====<br />
<pre><br />
\copy (SELECT * FROM products WHERE product_name LIKE 'SomePreFix%') To '/path/to/store/csv-file.csv' With CSV HEADER; <br />
</pre><br />
<br />
====Terminate any processing user of database====<br />
* To terminate all the processing users of the database, run the following script.<br />
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='DB_NAME';<br />
<br />
===Restart Server===<br />
* To restart the server, /etc/init.d/postgresql-8.x (depending on the version).<br />
sudo /etc/init.d/postgresql-8.4 restart<br />
<br />
==Install pgAdmin III==<br />
<pre><br />
$ sudo apt-get install pgadmin3 <br />
</pre><br />
<br />
==Troubleshooting==<br />
===Shared Memory Segment Issue===<br />
* http://askubuntu.com/questions/44373/how-to-fix-postgresql-installation<br />
* http://leopard.in.ua/2013/09/05/postgresql-sessting-shared-memory<br />
<br />
==References==<br />
{{Reflist|1}}</div>Kevinhttp://kevinlee.io/wiki/PostgreSQLPostgreSQL2016-01-01T15:43:20Z<p>Kevin: /* Back Up Database */</p>
<hr />
<div>[[Category:Linux]]<br />
[[Category:Software Development]]<br />
[[Category:Database]]<br />
{{Infobox software<br />
| name = PostgreSQL<br />
| logo = <br />
| screenshot =<br />
| caption =<br />
| developer = PostgreSQL Global Development Group<br />
| latest release version = 9.2.2,<br />9.1.7,<br />9.0.11,<br />8.4.15,<br />8.3.22<br />
| latest release date = <br />{{Start date and age|2012|12|06}}<br />
| latest preview version = <!--Add alpha, when it becomes available--><br />
| latest preview date = <!--{{Start date and age|YYYY|MM|DD}}--><br />
| operating system = [[Cross-platform]]<br />
| programming language = [[C (programming language)|C]]<br />
| genre = [[Object-relational database|ORDBMS]]<br />
| license = PostgreSQL licence<ref>{{cite web |date=2010-02-18|title=PostgreSQL licence approved by OSI| url= http://www.crynwr.com/cgi-bin/ezmlm-cgi?17:mmp:969| publisher = Crynwr | accessdate = 2010-02-18}}</ref><ref>{{cite web | publisher = Open Source Initiative | date = 2010-02-20|title=OSI PostgreSQL licence|url=http://www.opensource.org/licenses/postgresql | accessdate = 2010-02-20}}</ref><ref>{{cite web|url= http://www.postgresql.org/about/licence|title= License| publisher = PostgreSQL Global Development Group|accessdate=2010-09-20}}</ref><br />
| website = {{URL|www.postgresql.org}}<br />
}}<br />
<br />
==PostgreSQL==<br />
[http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] is an object-relational database management system (ORDBMS). The documentation of PostgreSQL 8.4.4 can be found [http://www.postgresql.org/docs/8.4/static here]<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static | title = PostgreSQL 8.4.4 Documentation | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
==Installation==<br />
*Install PostgreSQL<br />
<pre><br />
$ sudo apt-get install postgresql <br />
</pre><br />
<br />
=== For Latest Version ===<br />
<pre><br />
vim /etc/apt/sources.list.d/pgdg.list <br />
</pre><br />
<br />
add<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ YOUR_UBUNTU_VERSION_HERE-pgdg main <br />
</pre><br />
e.g.) 14.04<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main <br />
</pre><br />
<br />
Then import the repository signing key.<br />
<pre><br />
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \<br />
sudo apt-key add - <br />
</pre><br />
<br />
Now update and install.<br />
<pre><br />
apt-get update <br />
apt-get install postgresql <br />
</pre><br />
<br />
* Reference: http://www.postgresql.org/download/linux/ubuntu/<br />
<br />
==Administration==<br />
=== Setup ===<br />
====Setup Authentication Methods====<br />
* In order to allow new users to log in the PostgreSQL server, <code>/etc/postgresql/8.4/main/pg_hba.conf</code> should be modified.<br />
<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all ident<br />
# IPv4 local connections:<br />
host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
host all all ::1/128 md5<br />
To<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all md5<br />
# IPv4 local connections:<br />
#host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
#host all all ::1/128 md5<br />
Allowing the local access only with username & password.<br />
<br />
<br />
==== Access Remotely ====<br />
* Edit <code>/etc/postgresql/9.1/main/postgresql.conf</code><br />
<pre><br />
listen_addresses = 'localhost,address,separated,by,comma'<br />
</pre><br />
'''The IP is the local one but NOT a remote one'''.<br />
e.g.) 192.168.0.111 means it listens any access to 192.168.0.111 (local IP).<br />
<br />
e.g.)<br />
<pre><br />
listen_addresses = 'localhost,192.168.0.111'<br />
</pre><br />
<br />
* Edit <code>/etc/postgresql/9.1/main/pg_hba.conf</code><br />
<pre><br />
host all all IP/HERE trust<br />
</pre><br />
e.g.)<br />
<pre><br />
host all all 192.168.0.121/32 trust<br />
</pre><br />
'''The IP is not the local one but a remote one'''.<br />
e.g.) 192.168.0.121 means access from 192.168.0.121 is allowed.<br />
<br />
* Test if it works.<br />
<pre><br />
# -W: force password prompt<br />
$ psql -h IP-TO-DB-SERVER -U USERNAME -W <br />
</pre><br />
e.g.)<br />
<pre><br />
$ psql -h 192.168.0.123 -U db_user -W <br />
</pre><br />
<br />
===Roles (Users)===<br />
====Setup Root User Password====<br />
*Set up the password of the PostgreSQL root user<br />
$ sudo -u postgres psql postgres <br />
<br />
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.<br />
<br />
Type: \copyright for distribution terms<br />
\h for help with SQL commands<br />
\? for help with psql commands<br />
\g or terminate with semicolon to execute query<br />
\q to quit<br />
<br />
postgres=# '''ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';''' <br />
or<br />
postgres=# '''\password postgres''' <br />
<br />
Use <code>\quit</code> or <code>\q</code> to quit psql.<br />
postgres=# \q <br />
<br />
<br />
====Create Role (User)====<br />
Creating a user in PostgreSQL is in fact creating a role. So there are two ways to do it. The first one is using <code>CREATE ROLE</code><ref>{{cite web |url=http://www.postgresql.org/docs/8.4/static/database-roles.html | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles | date = 2009 | accessdate = 2010-08-02}}</ref> SQL command and the other one is using the programme, <code>createuser</code><ref>{{cite web|url= http://www.postgresql.org/docs/8.4/static/app-createuser.html | title= PostgreSQL 8.4.4 Documentation - createuser | date = 2009 | accessdate = 2010-08-02}}</ref>.<br />
<br />
=====CREATE ROLE=====<br />
<code>CREATE ROLE</code><ref>{{cite web |url = http://www.postgresql.org/docs/8.4/static/sql-createrole.html | title = PostgreSQL 8.4.4 Documentation - CREATE ROLE | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
CREATE ROLE role_name; <br />
CREATE ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
CREATE USER role_name WITH ENCRYPTED PASSWORD 'password'; <br />
(CREATE USER == CREATE ROLE + LOGIN) <br />
<br />
'''Usually use this.'''<br />
CREATE ROLE "role_name" WITH LOGIN ENCRYPTED PASSWORD 'password';<br />
<br />
=====createuser=====<br />
createuser [option...] [username] <br />
<br />
So to create a new user and a db belonging to him/her.<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-P: it will ask you to enter the password for the new user ('''role''').<br />
<br />
<br />
* Options<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createuser.html#AEN67555 | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles#Options | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
createuser accepts the following command-line arguments:<br />
<br />
username<br />
<br />
Specifies the name of the PostgreSQL user to be created. This name must be different from all existing roles in this PostgreSQL installation. <br />
-s<br />
--superuser<br />
<br />
The new user will be a superuser. <br />
-S<br />
--no-superuser<br />
<br />
The new user will not be a superuser. This is the default. <br />
-d<br />
--createdb<br />
<br />
The new user will be allowed to create databases. <br />
-D<br />
--no-createdb<br />
<br />
The new user will not be allowed to create databases. This is the default. <br />
-r<br />
--createrole<br />
<br />
The new user will be allowed to create new roles (that is, this user will have CREATEROLE privilege). <br />
-R<br />
--no-createrole<br />
<br />
The new user will not be allowed to create new roles. This is the default. <br />
-l<br />
--login<br />
<br />
The new user will be allowed to log in (that is, the user name can be used as the initial session user identifier). This is the default. <br />
-L<br />
--no-login<br />
<br />
The new user will not be allowed to log in. (A role without login privilege is still useful as a means of managing database permissions.) <br />
-i<br />
--inherit<br />
<br />
The new role will automatically inherit privileges of roles it is a member of. This is the default. <br />
-I<br />
--no-inherit<br />
<br />
The new role will not automatically inherit privileges of roles it is a member of. <br />
-c number<br />
--connection-limit number<br />
<br />
Set a maximum number of connections for the new user. The default is to set no limit. <br />
-P<br />
--pwprompt<br />
<br />
If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication. <br />
-E<br />
--encrypted<br />
<br />
Encrypts the user's password stored in the database. If not specified, the default password behavior is used. <br />
-N<br />
--unencrypted<br />
<br />
Does not encrypt the user's password stored in the database. If not specified, the default password behavior is used. <br />
-e<br />
--echo<br />
<br />
Echo the commands that createuser generates and sends to the server. <br />
<br />
You will be prompted for a name and other missing information if it is not specified on the command line.<br />
<br />
createuser also accepts the following command-line arguments for connection parameters:<br />
<br />
-h host<br />
--host host<br />
<br />
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. <br />
-p port<br />
--port port<br />
<br />
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. <br />
-U username<br />
--username username<br />
<br />
User name to connect as (not the user name to create). <br />
-w<br />
--no-password<br />
<br />
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. <br />
-W<br />
--password<br />
<br />
Force createuser to prompt for a password (for connecting to the server, not for the password of the new user).<br />
<br />
This option is never essential, since createuser will automatically prompt for a password if the server demands password authentication. However, createuser will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt. <br />
<br />
<br />
====View Roles====<br />
<br />
* To see all the exising roles.<br />
SELECT * FROM pg_roles;<br />
<br />
* To see the details of an existing role<br />
SELECT role_name FROM pg_roles; <br />
<br />
<br />
====Change Role====<br />
* <code>ALTER ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-alterrole.html | title = PostgreSQL 8.4.4 Documentation - ALTER ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
ALTER ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
<br />
<br />
====Remove Role====<br />
* Using <code>DROP ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-droprole.html | title = PostgreSQL 8.4.4 Documentation - DROP ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
DROP ROLE role_name; <br />
<br />
*Using <code>dropuser</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-dropuser.html | title = PostgreSQL 8.4.4 Documentation - dropuser | date = 2009 | accessdate = 2010-08-02}}</ref><br />
dropuser name <br />
<br />
===Databases===<br />
====List Databases====<br />
postgres=# \l <br />
<br />
====Select (Change) Database====<br />
postgres=# \c DB_NAME <br />
<br />
====List Table====<br />
<br />
postgres=# \d <br />
<br />
====Display Table Description====<br />
<br />
postgres=# \d TABLE_NAME <br />
<br />
<br />
====Creating Database====<br />
* Using <code>CREATE DATABASE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-createdatabase.html | title = PostgreSQL 8.4.4 Documentation - CREATE DATABASE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL to create a new database.<br />
CREATE DATABASE "db_name" <br />
<br />
CREATE DATABASE "db_name" OWNER "role_name" <br />
<br />
* Similar to creating a role, it is also possible to create a new database from the shell by using <code>createdb</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createdb.html | title = PostgreSQL 8.4.4 Documentation - createdb | date = 2009 | accessdate = 2010-08-02}}</ref> command.<br />
createdb [option...] [db_name] [description]<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-O owner<br />
--owner owner<br />
Specifies the database user who will own the new database.<br />
<br />
====Change Database====<br />
e.g.)<br />
postgres=# ALTER DATABASE db_name OWNER TO new_owner; <br />
<br />
====Privileges====<br />
=====GRANT=====<br />
e.g.)<br />
postgres=# GRANT ALL PRIVILEGES ON DATABASE db_name TO username; <br />
<br />
<br />
====Drop and Recreate database====<br />
* Access db<br />
$ sudo -u postgres psql postgres <br />
And drop and recreate the database.<br />
DROP DATABASE DB_NAME; <br />
CREATE DATABASE DB_NAME OWNER ROLE_NAME; <br />
<br />
====Back Up Database====<br />
$ sudo -u postgres pg_dump DB_NAME > DB_DUMP_DB_NAME.sql<br />
<br />
To custom-format archive file<br />
<pre><br />
pg_dump -Fc DB_NAME > "FILENAME"<br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_dump -Fc my_db > "my_db_backup.dump.compressed" <br />
</pre><br />
<br />
* Backup the entire contents of DB cluster<br />
<pre><br />
pg_dumpall > FILE <br />
</pre><br />
<br />
====Restore Database====<br />
$ sudo -u postgres psql --set ON_ERROR_STOP=on DB_NAME < DB_DUMP_DB_NAME.sql <br />
<br />
<pre><br />
pg_restore --no-owner -U USER -h HOST -d DB_NAME FILENAME <br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_restore --no-owner -U my_db_user -h HOST -d my_db "my_db_backup.dump.compressed" <br />
</pre><br />
<br />
<br />
====Run SQL Script====<br />
* Use psql terminal and select DB.<br />
$ sudo -u postgres psql <br />
postgres=# \c DB_NAME <br />
OR Access the DB directly<br />
$ sudo -u postgres psql DB_NAME <br />
DB_NAME=# <br />
<br />
* Run SQL script from within psql<br />
DB_NAME=# \set ON_ERROR_STOP <br />
DB_NAME=# \i db.sql<br />
<br />
* Run SQL query<br />
DB_NAME=# type SQL; <br />
e.g.)<br />
DB_NAME=# SELECT * FROM some_table;<br />
<br />
====Store the Query Result in File====<br />
DB_NAME=# \o file_name <br />
# Run some query<br />
DB_NAME=# SELECT * FROM some_table; <br />
# \o to finish<br />
DB_NAME=# \o<br />
==== Store Query Result in CSV file ====<br />
<pre><br />
\copy (SELECT * FROM products WHERE product_name LIKE 'SomePreFix%') To '/path/to/store/csv-file.csv' With CSV HEADER; <br />
</pre><br />
<br />
====Terminate any processing user of database====<br />
* To terminate all the processing users of the database, run the following script.<br />
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='DB_NAME';<br />
<br />
===Restart Server===<br />
* To restart the server, /etc/init.d/postgresql-8.x (depending on the version).<br />
sudo /etc/init.d/postgresql-8.4 restart<br />
<br />
==Install pgAdmin III==<br />
<pre><br />
$ sudo apt-get install pgadmin3 <br />
</pre><br />
<br />
==Troubleshooting==<br />
===Shared Memory Segment Issue===<br />
* http://askubuntu.com/questions/44373/how-to-fix-postgresql-installation<br />
* http://leopard.in.ua/2013/09/05/postgresql-sessting-shared-memory<br />
<br />
==References==<br />
{{Reflist|1}}</div>Kevinhttp://kevinlee.io/wiki/PostgreSQLPostgreSQL2016-01-01T15:33:20Z<p>Kevin: /* Back Up Database */</p>
<hr />
<div>[[Category:Linux]]<br />
[[Category:Software Development]]<br />
[[Category:Database]]<br />
{{Infobox software<br />
| name = PostgreSQL<br />
| logo = <br />
| screenshot =<br />
| caption =<br />
| developer = PostgreSQL Global Development Group<br />
| latest release version = 9.2.2,<br />9.1.7,<br />9.0.11,<br />8.4.15,<br />8.3.22<br />
| latest release date = <br />{{Start date and age|2012|12|06}}<br />
| latest preview version = <!--Add alpha, when it becomes available--><br />
| latest preview date = <!--{{Start date and age|YYYY|MM|DD}}--><br />
| operating system = [[Cross-platform]]<br />
| programming language = [[C (programming language)|C]]<br />
| genre = [[Object-relational database|ORDBMS]]<br />
| license = PostgreSQL licence<ref>{{cite web |date=2010-02-18|title=PostgreSQL licence approved by OSI| url= http://www.crynwr.com/cgi-bin/ezmlm-cgi?17:mmp:969| publisher = Crynwr | accessdate = 2010-02-18}}</ref><ref>{{cite web | publisher = Open Source Initiative | date = 2010-02-20|title=OSI PostgreSQL licence|url=http://www.opensource.org/licenses/postgresql | accessdate = 2010-02-20}}</ref><ref>{{cite web|url= http://www.postgresql.org/about/licence|title= License| publisher = PostgreSQL Global Development Group|accessdate=2010-09-20}}</ref><br />
| website = {{URL|www.postgresql.org}}<br />
}}<br />
<br />
==PostgreSQL==<br />
[http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] is an object-relational database management system (ORDBMS). The documentation of PostgreSQL 8.4.4 can be found [http://www.postgresql.org/docs/8.4/static here]<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static | title = PostgreSQL 8.4.4 Documentation | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
==Installation==<br />
*Install PostgreSQL<br />
<pre><br />
$ sudo apt-get install postgresql <br />
</pre><br />
<br />
=== For Latest Version ===<br />
<pre><br />
vim /etc/apt/sources.list.d/pgdg.list <br />
</pre><br />
<br />
add<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ YOUR_UBUNTU_VERSION_HERE-pgdg main <br />
</pre><br />
e.g.) 14.04<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main <br />
</pre><br />
<br />
Then import the repository signing key.<br />
<pre><br />
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \<br />
sudo apt-key add - <br />
</pre><br />
<br />
Now update and install.<br />
<pre><br />
apt-get update <br />
apt-get install postgresql <br />
</pre><br />
<br />
* Reference: http://www.postgresql.org/download/linux/ubuntu/<br />
<br />
==Administration==<br />
=== Setup ===<br />
====Setup Authentication Methods====<br />
* In order to allow new users to log in the PostgreSQL server, <code>/etc/postgresql/8.4/main/pg_hba.conf</code> should be modified.<br />
<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all ident<br />
# IPv4 local connections:<br />
host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
host all all ::1/128 md5<br />
To<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all md5<br />
# IPv4 local connections:<br />
#host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
#host all all ::1/128 md5<br />
Allowing the local access only with username & password.<br />
<br />
<br />
==== Access Remotely ====<br />
* Edit <code>/etc/postgresql/9.1/main/postgresql.conf</code><br />
<pre><br />
listen_addresses = 'localhost,address,separated,by,comma'<br />
</pre><br />
'''The IP is the local one but NOT a remote one'''.<br />
e.g.) 192.168.0.111 means it listens any access to 192.168.0.111 (local IP).<br />
<br />
e.g.)<br />
<pre><br />
listen_addresses = 'localhost,192.168.0.111'<br />
</pre><br />
<br />
* Edit <code>/etc/postgresql/9.1/main/pg_hba.conf</code><br />
<pre><br />
host all all IP/HERE trust<br />
</pre><br />
e.g.)<br />
<pre><br />
host all all 192.168.0.121/32 trust<br />
</pre><br />
'''The IP is not the local one but a remote one'''.<br />
e.g.) 192.168.0.121 means access from 192.168.0.121 is allowed.<br />
<br />
* Test if it works.<br />
<pre><br />
# -W: force password prompt<br />
$ psql -h IP-TO-DB-SERVER -U USERNAME -W <br />
</pre><br />
e.g.)<br />
<pre><br />
$ psql -h 192.168.0.123 -U db_user -W <br />
</pre><br />
<br />
===Roles (Users)===<br />
====Setup Root User Password====<br />
*Set up the password of the PostgreSQL root user<br />
$ sudo -u postgres psql postgres <br />
<br />
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.<br />
<br />
Type: \copyright for distribution terms<br />
\h for help with SQL commands<br />
\? for help with psql commands<br />
\g or terminate with semicolon to execute query<br />
\q to quit<br />
<br />
postgres=# '''ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';''' <br />
or<br />
postgres=# '''\password postgres''' <br />
<br />
Use <code>\quit</code> or <code>\q</code> to quit psql.<br />
postgres=# \q <br />
<br />
<br />
====Create Role (User)====<br />
Creating a user in PostgreSQL is in fact creating a role. So there are two ways to do it. The first one is using <code>CREATE ROLE</code><ref>{{cite web |url=http://www.postgresql.org/docs/8.4/static/database-roles.html | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles | date = 2009 | accessdate = 2010-08-02}}</ref> SQL command and the other one is using the programme, <code>createuser</code><ref>{{cite web|url= http://www.postgresql.org/docs/8.4/static/app-createuser.html | title= PostgreSQL 8.4.4 Documentation - createuser | date = 2009 | accessdate = 2010-08-02}}</ref>.<br />
<br />
=====CREATE ROLE=====<br />
<code>CREATE ROLE</code><ref>{{cite web |url = http://www.postgresql.org/docs/8.4/static/sql-createrole.html | title = PostgreSQL 8.4.4 Documentation - CREATE ROLE | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
CREATE ROLE role_name; <br />
CREATE ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
CREATE USER role_name WITH ENCRYPTED PASSWORD 'password'; <br />
(CREATE USER == CREATE ROLE + LOGIN) <br />
<br />
'''Usually use this.'''<br />
CREATE ROLE "role_name" WITH LOGIN ENCRYPTED PASSWORD 'password';<br />
<br />
=====createuser=====<br />
createuser [option...] [username] <br />
<br />
So to create a new user and a db belonging to him/her.<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-P: it will ask you to enter the password for the new user ('''role''').<br />
<br />
<br />
* Options<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createuser.html#AEN67555 | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles#Options | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
createuser accepts the following command-line arguments:<br />
<br />
username<br />
<br />
Specifies the name of the PostgreSQL user to be created. This name must be different from all existing roles in this PostgreSQL installation. <br />
-s<br />
--superuser<br />
<br />
The new user will be a superuser. <br />
-S<br />
--no-superuser<br />
<br />
The new user will not be a superuser. This is the default. <br />
-d<br />
--createdb<br />
<br />
The new user will be allowed to create databases. <br />
-D<br />
--no-createdb<br />
<br />
The new user will not be allowed to create databases. This is the default. <br />
-r<br />
--createrole<br />
<br />
The new user will be allowed to create new roles (that is, this user will have CREATEROLE privilege). <br />
-R<br />
--no-createrole<br />
<br />
The new user will not be allowed to create new roles. This is the default. <br />
-l<br />
--login<br />
<br />
The new user will be allowed to log in (that is, the user name can be used as the initial session user identifier). This is the default. <br />
-L<br />
--no-login<br />
<br />
The new user will not be allowed to log in. (A role without login privilege is still useful as a means of managing database permissions.) <br />
-i<br />
--inherit<br />
<br />
The new role will automatically inherit privileges of roles it is a member of. This is the default. <br />
-I<br />
--no-inherit<br />
<br />
The new role will not automatically inherit privileges of roles it is a member of. <br />
-c number<br />
--connection-limit number<br />
<br />
Set a maximum number of connections for the new user. The default is to set no limit. <br />
-P<br />
--pwprompt<br />
<br />
If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication. <br />
-E<br />
--encrypted<br />
<br />
Encrypts the user's password stored in the database. If not specified, the default password behavior is used. <br />
-N<br />
--unencrypted<br />
<br />
Does not encrypt the user's password stored in the database. If not specified, the default password behavior is used. <br />
-e<br />
--echo<br />
<br />
Echo the commands that createuser generates and sends to the server. <br />
<br />
You will be prompted for a name and other missing information if it is not specified on the command line.<br />
<br />
createuser also accepts the following command-line arguments for connection parameters:<br />
<br />
-h host<br />
--host host<br />
<br />
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. <br />
-p port<br />
--port port<br />
<br />
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. <br />
-U username<br />
--username username<br />
<br />
User name to connect as (not the user name to create). <br />
-w<br />
--no-password<br />
<br />
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. <br />
-W<br />
--password<br />
<br />
Force createuser to prompt for a password (for connecting to the server, not for the password of the new user).<br />
<br />
This option is never essential, since createuser will automatically prompt for a password if the server demands password authentication. However, createuser will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt. <br />
<br />
<br />
====View Roles====<br />
<br />
* To see all the exising roles.<br />
SELECT * FROM pg_roles;<br />
<br />
* To see the details of an existing role<br />
SELECT role_name FROM pg_roles; <br />
<br />
<br />
====Change Role====<br />
* <code>ALTER ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-alterrole.html | title = PostgreSQL 8.4.4 Documentation - ALTER ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
ALTER ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
<br />
<br />
====Remove Role====<br />
* Using <code>DROP ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-droprole.html | title = PostgreSQL 8.4.4 Documentation - DROP ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
DROP ROLE role_name; <br />
<br />
*Using <code>dropuser</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-dropuser.html | title = PostgreSQL 8.4.4 Documentation - dropuser | date = 2009 | accessdate = 2010-08-02}}</ref><br />
dropuser name <br />
<br />
===Databases===<br />
====List Databases====<br />
postgres=# \l <br />
<br />
====Select (Change) Database====<br />
postgres=# \c DB_NAME <br />
<br />
====List Table====<br />
<br />
postgres=# \d <br />
<br />
====Display Table Description====<br />
<br />
postgres=# \d TABLE_NAME <br />
<br />
<br />
====Creating Database====<br />
* Using <code>CREATE DATABASE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-createdatabase.html | title = PostgreSQL 8.4.4 Documentation - CREATE DATABASE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL to create a new database.<br />
CREATE DATABASE "db_name" <br />
<br />
CREATE DATABASE "db_name" OWNER "role_name" <br />
<br />
* Similar to creating a role, it is also possible to create a new database from the shell by using <code>createdb</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createdb.html | title = PostgreSQL 8.4.4 Documentation - createdb | date = 2009 | accessdate = 2010-08-02}}</ref> command.<br />
createdb [option...] [db_name] [description]<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-O owner<br />
--owner owner<br />
Specifies the database user who will own the new database.<br />
<br />
====Change Database====<br />
e.g.)<br />
postgres=# ALTER DATABASE db_name OWNER TO new_owner; <br />
<br />
====Privileges====<br />
=====GRANT=====<br />
e.g.)<br />
postgres=# GRANT ALL PRIVILEGES ON DATABASE db_name TO username; <br />
<br />
<br />
====Drop and Recreate database====<br />
* Access db<br />
$ sudo -u postgres psql postgres <br />
And drop and recreate the database.<br />
DROP DATABASE DB_NAME; <br />
CREATE DATABASE DB_NAME OWNER ROLE_NAME; <br />
<br />
====Back Up Database====<br />
$ sudo -u postgres pg_dump DB_NAME > DB_DUMP_DB_NAME.sql<br />
<br />
To custom-format archive file<br />
<pre><br />
pg_dump -Fc DB_NAME > "FILENAME"<br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_dump -Fc my_db > "my_db_backup.dump.compressed" <br />
</pre><br />
<br />
====Restore Database====<br />
$ sudo -u postgres psql --set ON_ERROR_STOP=on DB_NAME < DB_DUMP_DB_NAME.sql <br />
<br />
<pre><br />
pg_restore --no-owner -U USER -h HOST -d DB_NAME FILENAME <br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_restore --no-owner -U my_db_user -h HOST -d my_db "my_db_backup.dump.compressed" <br />
</pre><br />
<br />
<br />
====Run SQL Script====<br />
* Use psql terminal and select DB.<br />
$ sudo -u postgres psql <br />
postgres=# \c DB_NAME <br />
OR Access the DB directly<br />
$ sudo -u postgres psql DB_NAME <br />
DB_NAME=# <br />
<br />
* Run SQL script from within psql<br />
DB_NAME=# \set ON_ERROR_STOP <br />
DB_NAME=# \i db.sql<br />
<br />
* Run SQL query<br />
DB_NAME=# type SQL; <br />
e.g.)<br />
DB_NAME=# SELECT * FROM some_table;<br />
<br />
====Store the Query Result in File====<br />
DB_NAME=# \o file_name <br />
# Run some query<br />
DB_NAME=# SELECT * FROM some_table; <br />
# \o to finish<br />
DB_NAME=# \o<br />
==== Store Query Result in CSV file ====<br />
<pre><br />
\copy (SELECT * FROM products WHERE product_name LIKE 'SomePreFix%') To '/path/to/store/csv-file.csv' With CSV HEADER; <br />
</pre><br />
<br />
====Terminate any processing user of database====<br />
* To terminate all the processing users of the database, run the following script.<br />
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='DB_NAME';<br />
<br />
===Restart Server===<br />
* To restart the server, /etc/init.d/postgresql-8.x (depending on the version).<br />
sudo /etc/init.d/postgresql-8.4 restart<br />
<br />
==Install pgAdmin III==<br />
<pre><br />
$ sudo apt-get install pgadmin3 <br />
</pre><br />
<br />
==Troubleshooting==<br />
===Shared Memory Segment Issue===<br />
* http://askubuntu.com/questions/44373/how-to-fix-postgresql-installation<br />
* http://leopard.in.ua/2013/09/05/postgresql-sessting-shared-memory<br />
<br />
==References==<br />
{{Reflist|1}}</div>Kevinhttp://kevinlee.io/wiki/PostgreSQLPostgreSQL2016-01-01T15:29:04Z<p>Kevin: /* Databases */</p>
<hr />
<div>[[Category:Linux]]<br />
[[Category:Software Development]]<br />
[[Category:Database]]<br />
{{Infobox software<br />
| name = PostgreSQL<br />
| logo = <br />
| screenshot =<br />
| caption =<br />
| developer = PostgreSQL Global Development Group<br />
| latest release version = 9.2.2,<br />9.1.7,<br />9.0.11,<br />8.4.15,<br />8.3.22<br />
| latest release date = <br />{{Start date and age|2012|12|06}}<br />
| latest preview version = <!--Add alpha, when it becomes available--><br />
| latest preview date = <!--{{Start date and age|YYYY|MM|DD}}--><br />
| operating system = [[Cross-platform]]<br />
| programming language = [[C (programming language)|C]]<br />
| genre = [[Object-relational database|ORDBMS]]<br />
| license = PostgreSQL licence<ref>{{cite web |date=2010-02-18|title=PostgreSQL licence approved by OSI| url= http://www.crynwr.com/cgi-bin/ezmlm-cgi?17:mmp:969| publisher = Crynwr | accessdate = 2010-02-18}}</ref><ref>{{cite web | publisher = Open Source Initiative | date = 2010-02-20|title=OSI PostgreSQL licence|url=http://www.opensource.org/licenses/postgresql | accessdate = 2010-02-20}}</ref><ref>{{cite web|url= http://www.postgresql.org/about/licence|title= License| publisher = PostgreSQL Global Development Group|accessdate=2010-09-20}}</ref><br />
| website = {{URL|www.postgresql.org}}<br />
}}<br />
<br />
==PostgreSQL==<br />
[http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] is an object-relational database management system (ORDBMS). The documentation of PostgreSQL 8.4.4 can be found [http://www.postgresql.org/docs/8.4/static here]<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static | title = PostgreSQL 8.4.4 Documentation | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
==Installation==<br />
*Install PostgreSQL<br />
<pre><br />
$ sudo apt-get install postgresql <br />
</pre><br />
<br />
=== For Latest Version ===<br />
<pre><br />
vim /etc/apt/sources.list.d/pgdg.list <br />
</pre><br />
<br />
add<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ YOUR_UBUNTU_VERSION_HERE-pgdg main <br />
</pre><br />
e.g.) 14.04<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main <br />
</pre><br />
<br />
Then import the repository signing key.<br />
<pre><br />
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \<br />
sudo apt-key add - <br />
</pre><br />
<br />
Now update and install.<br />
<pre><br />
apt-get update <br />
apt-get install postgresql <br />
</pre><br />
<br />
* Reference: http://www.postgresql.org/download/linux/ubuntu/<br />
<br />
==Administration==<br />
=== Setup ===<br />
====Setup Authentication Methods====<br />
* In order to allow new users to log in the PostgreSQL server, <code>/etc/postgresql/8.4/main/pg_hba.conf</code> should be modified.<br />
<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all ident<br />
# IPv4 local connections:<br />
host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
host all all ::1/128 md5<br />
To<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all md5<br />
# IPv4 local connections:<br />
#host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
#host all all ::1/128 md5<br />
Allowing the local access only with username & password.<br />
<br />
<br />
==== Access Remotely ====<br />
* Edit <code>/etc/postgresql/9.1/main/postgresql.conf</code><br />
<pre><br />
listen_addresses = 'localhost,address,separated,by,comma'<br />
</pre><br />
'''The IP is the local one but NOT a remote one'''.<br />
e.g.) 192.168.0.111 means it listens any access to 192.168.0.111 (local IP).<br />
<br />
e.g.)<br />
<pre><br />
listen_addresses = 'localhost,192.168.0.111'<br />
</pre><br />
<br />
* Edit <code>/etc/postgresql/9.1/main/pg_hba.conf</code><br />
<pre><br />
host all all IP/HERE trust<br />
</pre><br />
e.g.)<br />
<pre><br />
host all all 192.168.0.121/32 trust<br />
</pre><br />
'''The IP is not the local one but a remote one'''.<br />
e.g.) 192.168.0.121 means access from 192.168.0.121 is allowed.<br />
<br />
* Test if it works.<br />
<pre><br />
# -W: force password prompt<br />
$ psql -h IP-TO-DB-SERVER -U USERNAME -W <br />
</pre><br />
e.g.)<br />
<pre><br />
$ psql -h 192.168.0.123 -U db_user -W <br />
</pre><br />
<br />
===Roles (Users)===<br />
====Setup Root User Password====<br />
*Set up the password of the PostgreSQL root user<br />
$ sudo -u postgres psql postgres <br />
<br />
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.<br />
<br />
Type: \copyright for distribution terms<br />
\h for help with SQL commands<br />
\? for help with psql commands<br />
\g or terminate with semicolon to execute query<br />
\q to quit<br />
<br />
postgres=# '''ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';''' <br />
or<br />
postgres=# '''\password postgres''' <br />
<br />
Use <code>\quit</code> or <code>\q</code> to quit psql.<br />
postgres=# \q <br />
<br />
<br />
====Create Role (User)====<br />
Creating a user in PostgreSQL is in fact creating a role. So there are two ways to do it. The first one is using <code>CREATE ROLE</code><ref>{{cite web |url=http://www.postgresql.org/docs/8.4/static/database-roles.html | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles | date = 2009 | accessdate = 2010-08-02}}</ref> SQL command and the other one is using the programme, <code>createuser</code><ref>{{cite web|url= http://www.postgresql.org/docs/8.4/static/app-createuser.html | title= PostgreSQL 8.4.4 Documentation - createuser | date = 2009 | accessdate = 2010-08-02}}</ref>.<br />
<br />
=====CREATE ROLE=====<br />
<code>CREATE ROLE</code><ref>{{cite web |url = http://www.postgresql.org/docs/8.4/static/sql-createrole.html | title = PostgreSQL 8.4.4 Documentation - CREATE ROLE | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
CREATE ROLE role_name; <br />
CREATE ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
CREATE USER role_name WITH ENCRYPTED PASSWORD 'password'; <br />
(CREATE USER == CREATE ROLE + LOGIN) <br />
<br />
'''Usually use this.'''<br />
CREATE ROLE "role_name" WITH LOGIN ENCRYPTED PASSWORD 'password';<br />
<br />
=====createuser=====<br />
createuser [option...] [username] <br />
<br />
So to create a new user and a db belonging to him/her.<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-P: it will ask you to enter the password for the new user ('''role''').<br />
<br />
<br />
* Options<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createuser.html#AEN67555 | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles#Options | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
createuser accepts the following command-line arguments:<br />
<br />
username<br />
<br />
Specifies the name of the PostgreSQL user to be created. This name must be different from all existing roles in this PostgreSQL installation. <br />
-s<br />
--superuser<br />
<br />
The new user will be a superuser. <br />
-S<br />
--no-superuser<br />
<br />
The new user will not be a superuser. This is the default. <br />
-d<br />
--createdb<br />
<br />
The new user will be allowed to create databases. <br />
-D<br />
--no-createdb<br />
<br />
The new user will not be allowed to create databases. This is the default. <br />
-r<br />
--createrole<br />
<br />
The new user will be allowed to create new roles (that is, this user will have CREATEROLE privilege). <br />
-R<br />
--no-createrole<br />
<br />
The new user will not be allowed to create new roles. This is the default. <br />
-l<br />
--login<br />
<br />
The new user will be allowed to log in (that is, the user name can be used as the initial session user identifier). This is the default. <br />
-L<br />
--no-login<br />
<br />
The new user will not be allowed to log in. (A role without login privilege is still useful as a means of managing database permissions.) <br />
-i<br />
--inherit<br />
<br />
The new role will automatically inherit privileges of roles it is a member of. This is the default. <br />
-I<br />
--no-inherit<br />
<br />
The new role will not automatically inherit privileges of roles it is a member of. <br />
-c number<br />
--connection-limit number<br />
<br />
Set a maximum number of connections for the new user. The default is to set no limit. <br />
-P<br />
--pwprompt<br />
<br />
If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication. <br />
-E<br />
--encrypted<br />
<br />
Encrypts the user's password stored in the database. If not specified, the default password behavior is used. <br />
-N<br />
--unencrypted<br />
<br />
Does not encrypt the user's password stored in the database. If not specified, the default password behavior is used. <br />
-e<br />
--echo<br />
<br />
Echo the commands that createuser generates and sends to the server. <br />
<br />
You will be prompted for a name and other missing information if it is not specified on the command line.<br />
<br />
createuser also accepts the following command-line arguments for connection parameters:<br />
<br />
-h host<br />
--host host<br />
<br />
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. <br />
-p port<br />
--port port<br />
<br />
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. <br />
-U username<br />
--username username<br />
<br />
User name to connect as (not the user name to create). <br />
-w<br />
--no-password<br />
<br />
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. <br />
-W<br />
--password<br />
<br />
Force createuser to prompt for a password (for connecting to the server, not for the password of the new user).<br />
<br />
This option is never essential, since createuser will automatically prompt for a password if the server demands password authentication. However, createuser will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt. <br />
<br />
<br />
====View Roles====<br />
<br />
* To see all the exising roles.<br />
SELECT * FROM pg_roles;<br />
<br />
* To see the details of an existing role<br />
SELECT role_name FROM pg_roles; <br />
<br />
<br />
====Change Role====<br />
* <code>ALTER ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-alterrole.html | title = PostgreSQL 8.4.4 Documentation - ALTER ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
ALTER ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
<br />
<br />
====Remove Role====<br />
* Using <code>DROP ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-droprole.html | title = PostgreSQL 8.4.4 Documentation - DROP ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
DROP ROLE role_name; <br />
<br />
*Using <code>dropuser</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-dropuser.html | title = PostgreSQL 8.4.4 Documentation - dropuser | date = 2009 | accessdate = 2010-08-02}}</ref><br />
dropuser name <br />
<br />
===Databases===<br />
====List Databases====<br />
postgres=# \l <br />
<br />
====Select (Change) Database====<br />
postgres=# \c DB_NAME <br />
<br />
====List Table====<br />
<br />
postgres=# \d <br />
<br />
====Display Table Description====<br />
<br />
postgres=# \d TABLE_NAME <br />
<br />
<br />
====Creating Database====<br />
* Using <code>CREATE DATABASE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-createdatabase.html | title = PostgreSQL 8.4.4 Documentation - CREATE DATABASE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL to create a new database.<br />
CREATE DATABASE "db_name" <br />
<br />
CREATE DATABASE "db_name" OWNER "role_name" <br />
<br />
* Similar to creating a role, it is also possible to create a new database from the shell by using <code>createdb</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createdb.html | title = PostgreSQL 8.4.4 Documentation - createdb | date = 2009 | accessdate = 2010-08-02}}</ref> command.<br />
createdb [option...] [db_name] [description]<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-O owner<br />
--owner owner<br />
Specifies the database user who will own the new database.<br />
<br />
====Change Database====<br />
e.g.)<br />
postgres=# ALTER DATABASE db_name OWNER TO new_owner; <br />
<br />
====Privileges====<br />
=====GRANT=====<br />
e.g.)<br />
postgres=# GRANT ALL PRIVILEGES ON DATABASE db_name TO username; <br />
<br />
<br />
====Drop and Recreate database====<br />
* Access db<br />
$ sudo -u postgres psql postgres <br />
And drop and recreate the database.<br />
DROP DATABASE DB_NAME; <br />
CREATE DATABASE DB_NAME OWNER ROLE_NAME; <br />
<br />
====Back Up Database====<br />
$ sudo -u postgres pg_dump DB_NAME > DB_DUMP_DB_NAME.sql<br />
<br />
With compression<br />
<pre><br />
pg_dump -Fc DB_NAME > "FILENAME"<br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_dump -Fc my_db > "my_db_backup.dump.compressed" <br />
</pre><br />
<br />
====Restore Database====<br />
$ sudo -u postgres psql --set ON_ERROR_STOP=on DB_NAME < DB_DUMP_DB_NAME.sql <br />
<br />
<pre><br />
pg_restore --no-owner -U USER -h HOST -d DB_NAME FILENAME <br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_restore --no-owner -U my_db_user -h HOST -d my_db "my_db_backup.dump.compressed" <br />
</pre><br />
<br />
<br />
====Run SQL Script====<br />
* Use psql terminal and select DB.<br />
$ sudo -u postgres psql <br />
postgres=# \c DB_NAME <br />
OR Access the DB directly<br />
$ sudo -u postgres psql DB_NAME <br />
DB_NAME=# <br />
<br />
* Run SQL script from within psql<br />
DB_NAME=# \set ON_ERROR_STOP <br />
DB_NAME=# \i db.sql<br />
<br />
* Run SQL query<br />
DB_NAME=# type SQL; <br />
e.g.)<br />
DB_NAME=# SELECT * FROM some_table;<br />
<br />
====Store the Query Result in File====<br />
DB_NAME=# \o file_name <br />
# Run some query<br />
DB_NAME=# SELECT * FROM some_table; <br />
# \o to finish<br />
DB_NAME=# \o<br />
==== Store Query Result in CSV file ====<br />
<pre><br />
\copy (SELECT * FROM products WHERE product_name LIKE 'SomePreFix%') To '/path/to/store/csv-file.csv' With CSV HEADER; <br />
</pre><br />
<br />
====Terminate any processing user of database====<br />
* To terminate all the processing users of the database, run the following script.<br />
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='DB_NAME';<br />
<br />
===Restart Server===<br />
* To restart the server, /etc/init.d/postgresql-8.x (depending on the version).<br />
sudo /etc/init.d/postgresql-8.4 restart<br />
<br />
==Install pgAdmin III==<br />
<pre><br />
$ sudo apt-get install pgadmin3 <br />
</pre><br />
<br />
==Troubleshooting==<br />
===Shared Memory Segment Issue===<br />
* http://askubuntu.com/questions/44373/how-to-fix-postgresql-installation<br />
* http://leopard.in.ua/2013/09/05/postgresql-sessting-shared-memory<br />
<br />
==References==<br />
{{Reflist|1}}</div>Kevinhttp://kevinlee.io/wiki/PostgreSQLPostgreSQL2016-01-01T15:26:33Z<p>Kevin: /* Back Up Database */</p>
<hr />
<div>[[Category:Linux]]<br />
[[Category:Software Development]]<br />
[[Category:Database]]<br />
{{Infobox software<br />
| name = PostgreSQL<br />
| logo = <br />
| screenshot =<br />
| caption =<br />
| developer = PostgreSQL Global Development Group<br />
| latest release version = 9.2.2,<br />9.1.7,<br />9.0.11,<br />8.4.15,<br />8.3.22<br />
| latest release date = <br />{{Start date and age|2012|12|06}}<br />
| latest preview version = <!--Add alpha, when it becomes available--><br />
| latest preview date = <!--{{Start date and age|YYYY|MM|DD}}--><br />
| operating system = [[Cross-platform]]<br />
| programming language = [[C (programming language)|C]]<br />
| genre = [[Object-relational database|ORDBMS]]<br />
| license = PostgreSQL licence<ref>{{cite web |date=2010-02-18|title=PostgreSQL licence approved by OSI| url= http://www.crynwr.com/cgi-bin/ezmlm-cgi?17:mmp:969| publisher = Crynwr | accessdate = 2010-02-18}}</ref><ref>{{cite web | publisher = Open Source Initiative | date = 2010-02-20|title=OSI PostgreSQL licence|url=http://www.opensource.org/licenses/postgresql | accessdate = 2010-02-20}}</ref><ref>{{cite web|url= http://www.postgresql.org/about/licence|title= License| publisher = PostgreSQL Global Development Group|accessdate=2010-09-20}}</ref><br />
| website = {{URL|www.postgresql.org}}<br />
}}<br />
<br />
==PostgreSQL==<br />
[http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] is an object-relational database management system (ORDBMS). The documentation of PostgreSQL 8.4.4 can be found [http://www.postgresql.org/docs/8.4/static here]<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static | title = PostgreSQL 8.4.4 Documentation | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
==Installation==<br />
*Install PostgreSQL<br />
<pre><br />
$ sudo apt-get install postgresql <br />
</pre><br />
<br />
=== For Latest Version ===<br />
<pre><br />
vim /etc/apt/sources.list.d/pgdg.list <br />
</pre><br />
<br />
add<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ YOUR_UBUNTU_VERSION_HERE-pgdg main <br />
</pre><br />
e.g.) 14.04<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main <br />
</pre><br />
<br />
Then import the repository signing key.<br />
<pre><br />
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \<br />
sudo apt-key add - <br />
</pre><br />
<br />
Now update and install.<br />
<pre><br />
apt-get update <br />
apt-get install postgresql <br />
</pre><br />
<br />
* Reference: http://www.postgresql.org/download/linux/ubuntu/<br />
<br />
==Administration==<br />
=== Setup ===<br />
====Setup Authentication Methods====<br />
* In order to allow new users to log in the PostgreSQL server, <code>/etc/postgresql/8.4/main/pg_hba.conf</code> should be modified.<br />
<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all ident<br />
# IPv4 local connections:<br />
host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
host all all ::1/128 md5<br />
To<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all md5<br />
# IPv4 local connections:<br />
#host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
#host all all ::1/128 md5<br />
Allowing the local access only with username & password.<br />
<br />
<br />
==== Access Remotely ====<br />
* Edit <code>/etc/postgresql/9.1/main/postgresql.conf</code><br />
<pre><br />
listen_addresses = 'localhost,address,separated,by,comma'<br />
</pre><br />
'''The IP is the local one but NOT a remote one'''.<br />
e.g.) 192.168.0.111 means it listens any access to 192.168.0.111 (local IP).<br />
<br />
e.g.)<br />
<pre><br />
listen_addresses = 'localhost,192.168.0.111'<br />
</pre><br />
<br />
* Edit <code>/etc/postgresql/9.1/main/pg_hba.conf</code><br />
<pre><br />
host all all IP/HERE trust<br />
</pre><br />
e.g.)<br />
<pre><br />
host all all 192.168.0.121/32 trust<br />
</pre><br />
'''The IP is not the local one but a remote one'''.<br />
e.g.) 192.168.0.121 means access from 192.168.0.121 is allowed.<br />
<br />
* Test if it works.<br />
<pre><br />
# -W: force password prompt<br />
$ psql -h IP-TO-DB-SERVER -U USERNAME -W <br />
</pre><br />
e.g.)<br />
<pre><br />
$ psql -h 192.168.0.123 -U db_user -W <br />
</pre><br />
<br />
===Roles (Users)===<br />
====Setup Root User Password====<br />
*Set up the password of the PostgreSQL root user<br />
$ sudo -u postgres psql postgres <br />
<br />
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.<br />
<br />
Type: \copyright for distribution terms<br />
\h for help with SQL commands<br />
\? for help with psql commands<br />
\g or terminate with semicolon to execute query<br />
\q to quit<br />
<br />
postgres=# '''ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';''' <br />
or<br />
postgres=# '''\password postgres''' <br />
<br />
Use <code>\quit</code> or <code>\q</code> to quit psql.<br />
postgres=# \q <br />
<br />
<br />
====Create Role (User)====<br />
Creating a user in PostgreSQL is in fact creating a role. So there are two ways to do it. The first one is using <code>CREATE ROLE</code><ref>{{cite web |url=http://www.postgresql.org/docs/8.4/static/database-roles.html | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles | date = 2009 | accessdate = 2010-08-02}}</ref> SQL command and the other one is using the programme, <code>createuser</code><ref>{{cite web|url= http://www.postgresql.org/docs/8.4/static/app-createuser.html | title= PostgreSQL 8.4.4 Documentation - createuser | date = 2009 | accessdate = 2010-08-02}}</ref>.<br />
<br />
=====CREATE ROLE=====<br />
<code>CREATE ROLE</code><ref>{{cite web |url = http://www.postgresql.org/docs/8.4/static/sql-createrole.html | title = PostgreSQL 8.4.4 Documentation - CREATE ROLE | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
CREATE ROLE role_name; <br />
CREATE ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
CREATE USER role_name WITH ENCRYPTED PASSWORD 'password'; <br />
(CREATE USER == CREATE ROLE + LOGIN) <br />
<br />
'''Usually use this.'''<br />
CREATE ROLE "role_name" WITH LOGIN ENCRYPTED PASSWORD 'password';<br />
<br />
=====createuser=====<br />
createuser [option...] [username] <br />
<br />
So to create a new user and a db belonging to him/her.<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-P: it will ask you to enter the password for the new user ('''role''').<br />
<br />
<br />
* Options<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createuser.html#AEN67555 | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles#Options | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
createuser accepts the following command-line arguments:<br />
<br />
username<br />
<br />
Specifies the name of the PostgreSQL user to be created. This name must be different from all existing roles in this PostgreSQL installation. <br />
-s<br />
--superuser<br />
<br />
The new user will be a superuser. <br />
-S<br />
--no-superuser<br />
<br />
The new user will not be a superuser. This is the default. <br />
-d<br />
--createdb<br />
<br />
The new user will be allowed to create databases. <br />
-D<br />
--no-createdb<br />
<br />
The new user will not be allowed to create databases. This is the default. <br />
-r<br />
--createrole<br />
<br />
The new user will be allowed to create new roles (that is, this user will have CREATEROLE privilege). <br />
-R<br />
--no-createrole<br />
<br />
The new user will not be allowed to create new roles. This is the default. <br />
-l<br />
--login<br />
<br />
The new user will be allowed to log in (that is, the user name can be used as the initial session user identifier). This is the default. <br />
-L<br />
--no-login<br />
<br />
The new user will not be allowed to log in. (A role without login privilege is still useful as a means of managing database permissions.) <br />
-i<br />
--inherit<br />
<br />
The new role will automatically inherit privileges of roles it is a member of. This is the default. <br />
-I<br />
--no-inherit<br />
<br />
The new role will not automatically inherit privileges of roles it is a member of. <br />
-c number<br />
--connection-limit number<br />
<br />
Set a maximum number of connections for the new user. The default is to set no limit. <br />
-P<br />
--pwprompt<br />
<br />
If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication. <br />
-E<br />
--encrypted<br />
<br />
Encrypts the user's password stored in the database. If not specified, the default password behavior is used. <br />
-N<br />
--unencrypted<br />
<br />
Does not encrypt the user's password stored in the database. If not specified, the default password behavior is used. <br />
-e<br />
--echo<br />
<br />
Echo the commands that createuser generates and sends to the server. <br />
<br />
You will be prompted for a name and other missing information if it is not specified on the command line.<br />
<br />
createuser also accepts the following command-line arguments for connection parameters:<br />
<br />
-h host<br />
--host host<br />
<br />
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. <br />
-p port<br />
--port port<br />
<br />
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. <br />
-U username<br />
--username username<br />
<br />
User name to connect as (not the user name to create). <br />
-w<br />
--no-password<br />
<br />
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. <br />
-W<br />
--password<br />
<br />
Force createuser to prompt for a password (for connecting to the server, not for the password of the new user).<br />
<br />
This option is never essential, since createuser will automatically prompt for a password if the server demands password authentication. However, createuser will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt. <br />
<br />
<br />
====View Roles====<br />
<br />
* To see all the exising roles.<br />
SELECT * FROM pg_roles;<br />
<br />
* To see the details of an existing role<br />
SELECT role_name FROM pg_roles; <br />
<br />
<br />
====Change Role====<br />
* <code>ALTER ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-alterrole.html | title = PostgreSQL 8.4.4 Documentation - ALTER ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
ALTER ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
<br />
<br />
====Remove Role====<br />
* Using <code>DROP ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-droprole.html | title = PostgreSQL 8.4.4 Documentation - DROP ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
DROP ROLE role_name; <br />
<br />
*Using <code>dropuser</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-dropuser.html | title = PostgreSQL 8.4.4 Documentation - dropuser | date = 2009 | accessdate = 2010-08-02}}</ref><br />
dropuser name <br />
<br />
===Databases===<br />
====List Databases====<br />
postgres=# \l <br />
<br />
====Select (Change) Database====<br />
postgres=# \c DB_NAME <br />
<br />
====List Table====<br />
<br />
postgres=# \d <br />
<br />
====Display Table Description====<br />
<br />
postgres=# \d TABLE_NAME <br />
<br />
<br />
====Creating Database====<br />
* Using <code>CREATE DATABASE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-createdatabase.html | title = PostgreSQL 8.4.4 Documentation - CREATE DATABASE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL to create a new database.<br />
CREATE DATABASE "db_name" <br />
<br />
CREATE DATABASE "db_name" OWNER "role_name" <br />
<br />
* Similar to creating a role, it is also possible to create a new database from the shell by using <code>createdb</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createdb.html | title = PostgreSQL 8.4.4 Documentation - createdb | date = 2009 | accessdate = 2010-08-02}}</ref> command.<br />
createdb [option...] [db_name] [description]<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-O owner<br />
--owner owner<br />
Specifies the database user who will own the new database.<br />
<br />
====Change Database====<br />
e.g.)<br />
postgres=# ALTER DATABASE db_name OWNER TO new_owner; <br />
<br />
====Privileges====<br />
=====GRANT=====<br />
e.g.)<br />
postgres=# GRANT ALL PRIVILEGES ON DATABASE db_name TO username; <br />
<br />
<br />
====Drop and Recreate database====<br />
* Access db<br />
$ sudo -u postgres psql postgres <br />
And drop and recreate the database.<br />
DROP DATABASE DB_NAME; <br />
CREATE DATABASE DB_NAME OWNER ROLE_NAME; <br />
<br />
====Back Up Database====<br />
$ sudo -u postgres pg_dump DB_NAME > DB_DUMP_DB_NAME.sql<br />
<br />
With compression<br />
<pre><br />
pg_dump -Fc DB_NAME > "FILENAME"<br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ pg_dump -Fc my_db > "my_db_backup.dump.compressed"<br />
</pre><br />
<br />
====Restore Database====<br />
$ sudo -u postgres psql --set ON_ERROR_STOP=on DB_NAME < DB_DUMP_DB_NAME.sql <br />
<br />
<br />
====Run SQL Script====<br />
* Use psql terminal and select DB.<br />
$ sudo -u postgres psql <br />
postgres=# \c DB_NAME <br />
OR Access the DB directly<br />
$ sudo -u postgres psql DB_NAME <br />
DB_NAME=# <br />
<br />
* Run SQL script from within psql<br />
DB_NAME=# \set ON_ERROR_STOP <br />
DB_NAME=# \i db.sql<br />
<br />
* Run SQL query<br />
DB_NAME=# type SQL; <br />
e.g.)<br />
DB_NAME=# SELECT * FROM some_table;<br />
<br />
====Store the Query Result in File====<br />
DB_NAME=# \o file_name <br />
# Run some query<br />
DB_NAME=# SELECT * FROM some_table; <br />
# \o to finish<br />
DB_NAME=# \o<br />
==== Store Query Result in CSV file ====<br />
<pre><br />
\copy (SELECT * FROM products WHERE product_name LIKE 'SomePreFix%') To '/path/to/store/csv-file.csv' With CSV HEADER; <br />
</pre><br />
<br />
====Terminate any processing user of database====<br />
* To terminate all the processing users of the database, run the following script.<br />
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='DB_NAME';<br />
<br />
<br />
<br />
===Restart Server===<br />
* To restart the server, /etc/init.d/postgresql-8.x (depending on the version).<br />
sudo /etc/init.d/postgresql-8.4 restart<br />
<br />
==Install pgAdmin III==<br />
<pre><br />
$ sudo apt-get install pgadmin3 <br />
</pre><br />
<br />
==Troubleshooting==<br />
===Shared Memory Segment Issue===<br />
* http://askubuntu.com/questions/44373/how-to-fix-postgresql-installation<br />
* http://leopard.in.ua/2013/09/05/postgresql-sessting-shared-memory<br />
<br />
==References==<br />
{{Reflist|1}}</div>Kevinhttp://kevinlee.io/wiki/Main_PageMain Page2015-07-20T00:24:13Z<p>Kevin: </p>
<hr />
<div>[[Category:Root]]<br />
<big>This is Kevin's Wiki.</big><br />
<br />
'''*************************************************************************************************************************************'''<br /><br />
This Wiki is to store all the information and knowledge related to IT which I acquired from my study and work.<br /><br />
'''*************************************************************************************************************************************'''<br /><br />
<br />
'''WARNING!!! This wiki may contain some outdated information. The authour of this wiki is NOT responsible for any negative consequence caused by following the information found here.'''<br />
<br />
== My Wiki Category List ==<br />
<div style="background-color:#F9F9F9; border:1px solid #AAAAAA; clear:both; margin-top:1em; padding:5px;"><br />
*[[:Category:MediaWiki Related|MediaWiki Related]]<br />
*[[:Category:Linux|Linux]]<br />
*[[:Category:Software Development|Software Development]]<br />
*[[:Category:Weblog Software|Weblog Software]]<br />
</div><br />
<br />
<br />
<br />
<br />
Consult the [http://meta.wikimedia.org/wiki/Help:Contents User's Guide] for information on using the wiki software.<br />
<br />
== Getting started ==<br />
* [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br />
* [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br />
* [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]</div>Kevinhttp://kevinlee.io/wiki/Linux_User_ManagementLinux User Management2015-07-12T06:32:32Z<p>Kevin: </p>
<hr />
<div>[[Category:Linux]]<br />
== Add a new user on Linux ==<br />
<br />
=== Use THIS!!! ===<br />
Details:<br />
https://www.linode.com/docs/security/securing-your-server<br />
<br />
<pre><br />
$ adduser yourusername <br />
$ usermod -a -G sudo yourusername <br />
</pre><br />
Now, use the new username to log in.<br />
<br />
<br />
=== Essential ===<br />
One way to add a new user on Linux is using ''useradd'' command.<br />
<br />
<pre><br />
$ useradd <username> <br />
</pre><br />
# You probably need to use [[wikipedia:en:Sudo|Sudo]] command.<br />
$ sudo useradd <username> <br />
<br />
<br />
=== With Home Directory ===<br />
However, the user added will have neither the password nor the home directory.<br />
If you want to specify the home directory, you can use -d option. If you also want to create home directory and set it for the user when adding a new user, you can use -m option with -d.<br />
<br />
<pre><br />
$ useradd -d <home directory path> -m <username> <br />
<br />
$ useradd -d /home/newuser -m newuser <br />
</pre><br />
<br />
=== With Specifying Login Shell ===<br />
After using useradd above, you may realise that the new user's login shell is not the one you want him or her to have. So in order to specify the login shell, you can -s option.<br />
<br />
<pre><br />
$ useradd -d <home directory path> -m <username> -s <shell> <br />
<br />
$ useradd -d /home/newuser -m newuser -s /bin/bash <br />
</pre><br />
<br />
=== Set User Password ===<br />
<pre><br />
$ passwd USERNAME <br />
Enter new UNIX password: <br />
Retype new UNIX password: <br />
passwd: password updated successfully<br />
</pre><br />
<br />
To get valid login shell list, check out ''/etc/shells'' file.<br />
<pre><br />
$ cat /etc/shells <br />
# /etc/shells: valid login shells<br />
/bin/csh<br />
/usr/bin/es<br />
/usr/bin/ksh<br />
/bin/ksh<br />
/usr/bin/rc<br />
/usr/bin/tcsh<br />
/bin/tcsh<br />
/usr/bin/esh<br />
/usr/bin/screen<br />
/bin/sh<br />
/bin/dash<br />
/bin/false<br />
/bin/bash<br />
/bin/rbash<br />
</pre><br />
<br />
<br />
<br />
=== <code>useradd</code> Help Result ===<br />
<pre><br />
$ useradd --help <br />
Usage: useradd [options] LOGIN<br />
<br />
Options:<br />
-b, --base-dir BASE_DIR base directory for the new user account<br />
home directory<br />
-c, --comment COMMENT set the GECOS field for the new user account<br />
-d, --home-dir HOME_DIR home directory for the new user account<br />
-D, --defaults print or save modified default useradd<br />
configuration<br />
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE<br />
-f, --inactive INACTIVE set password inactive after expiration<br />
to INACTIVE<br />
-g, --gid GROUP force use GROUP for the new user account<br />
-G, --groups GROUPS list of supplementary groups for the new<br />
user account<br />
-h, --help display this help message and exit<br />
-k, --skel SKEL_DIR specify an alternative skel directory<br />
-K, --key KEY=VALUE overrides /etc/login.defs defaults<br />
-m, --create-home create home directory for the new user<br />
account<br />
-o, --non-unique allow create user with duplicate<br />
(non-unique) UID<br />
-p, --password PASSWORD use encrypted password for the new user<br />
account<br />
-r, --system create a system account<br />
-s, --shell SHELL the login shell for the new user account<br />
-u, --uid UID force use the UID for the new user account<br />
</pre><br />
<br />
<br />
== Modify User ==<br />
=== Change Home Directory ===<br />
<pre><br />
$ sudo usermod -d /userhome/path/ <username> <br />
</pre><br />
<br />
=== <code>usermod</code> Help Result ===<br />
<pre><br />
$ usermod --help <br />
Usage: usermod [options] LOGIN<br />
<br />
Options:<br />
-c, --comment COMMENT new value of the GECOS field<br />
-d, --home HOME_DIR new home directory for the user account<br />
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE<br />
-f, --inactive INACTIVE set password inactive after expiration<br />
to INACTIVE<br />
-g, --gid GROUP force use GROUP as new primary group<br />
-G, --groups GROUPS new list of supplementary GROUPS<br />
-a, --append append the user to the supplemental GROUPS<br />
mentioned by the -G option without removing<br />
him/her from other groups<br />
-h, --help display this help message and exit<br />
-l, --login NEW_LOGIN new value of the login name<br />
-L, --lock lock the user account<br />
-m, --move-home move contents of the home directory to the<br />
new location (use only with -d)<br />
-o, --non-unique allow using duplicate (non-unique) UID<br />
-p, --password PASSWORD use encrypted password for the new password<br />
-s, --shell SHELL new login shell for the user account<br />
-u, --uid UID new UID for the user account<br />
-U, --unlock unlock the user account<br />
</pre><br />
<br />
<br />
== Remove User ==<br />
=== Essential ===<br />
A simple way to remove the existing Linux user is using ''userdel'' command.<br />
<br />
<pre><br />
$ userdel <username> <br />
</pre><br />
# You probably need to use [[wikipedia:en:Sudo|Sudo]] command.<br />
$ sudo userdel <username> <br />
<br />
=== Also Remove Home Directory ===<br />
<br />
<pre><br />
$ userdel -r <username> <br />
$ userdel -r newuser <br />
</pre><br />
<br />
=== <code>userdel</code> Help Result ===<br />
<pre><br />
$ userdel --help <br />
Usage: userdel [options] LOGIN<br />
<br />
Options:<br />
-f, --force force removal of files, even if not owned by user<br />
-h, --help display this help message and exit<br />
-r, --remove remove home directory and mail spool<br />
</pre><br />
<br />
<br />
== Sudoer ==<br />
=== Add User to <code>sudo</code> List ===<br />
-To add a uer to sudo list, run the command below<br />
<br />
<pre><br />
$visudo <br />
</pre><br />
<br />
<br />
-Add the '''username''' after <code>root</code><br />
<br />
# User privilege specification<br />
root,'''username''' ALL=(ALL) ALL<br />
<code>i -> edit -> ESC -> : -> wq</code><br />
<br />
<br />
=== Disable root User ID ===<br />
<div style="background: #ffd; border: 1px solid"><br />
* '''Do not forget to have at least one sudoer user before running this.'''<br />
</div><br />
<pre><br />
$ passwd -l root <br />
</pre><br />
<br />
=== <code>passwd</code> Help Result ===<br />
<pre><br />
$ passwd --help <br />
Usage: passwd [options] [LOGIN]<br />
<br />
Options:<br />
-a, --all report password status on all accounts<br />
-d, --delete delete the password for the named account<br />
-e, --expire force expire the password for the named account<br />
-h, --help display this help message and exit<br />
-k, --keep-tokens change password only if expired<br />
-i, --inactive INACTIVE set password inactive after expiration<br />
to INACTIVE<br />
-l, --lock lock the named account<br />
-n, --mindays MIN_DAYS set minimum number of days before password<br />
change to MIN_DAYS<br />
-q, --quiet quiet mode<br />
-r, --repository REPOSITORY change password in REPOSITORY repository<br />
-S, --status report password status on the named account<br />
-u, --unlock unlock the named account<br />
-w, --warndays WARN_DAYS set expiration warning days to WARN_DAYS<br />
-x, --maxdays MAX_DAYS set maximim number of days before password<br />
change to MAX_DAYS<br />
</pre><br />
<br />
<br />
=== Root Login ===<br />
If you need a persistent root login, use<br />
<pre><br />
$ sudo -i <br />
</pre><br />
<br />
=== References ===<br />
[[https://help.ubuntu.com/community/RootSudo RootSudo]]<br />
<br />
<br />
==Kick Out User Logged in==<br />
===How to===<br />
-Check the processes of the user whom you want to kick out, then kill the user's login shell (e.g. bash)<br />
<br />
-To display all the processes belong to the user.<br />
<pre><br />
$ ps aux | grep ${USER} <br />
</pre><br />
<br />
-To display only PIDs and processes belong to the user<br />
<pre><br />
$ ps aux | grep ${USER} | awk '{print $2 " " $11}' | sort -gr <br />
</pre><br />
<br />
-To kill the process<br />
<pre><br />
$ sudo kill -s 9 <PID number> <br />
</pre><br />
<br />
<br />
=== Example ===<br />
<pre><br />
# username is 'user'<br />
<br />
$ ps aux | grep ${USER} <br />
root 19087 0.0 0.7 67964 2904 ? Ss 13:32 0:00 sshd: user [priv]<br />
user 19089 0.0 0.4 67964 1724 ? S 13:32 0:00 sshd: user@pts/0<br />
user 19090 0.0 0.6 19336 2248 pts/0 Ss 13:32 0:00 -bash<br />
user 19100 0.0 0.2 15056 1088 pts/0 R+ 13:32 0:00 ps aux<br />
user 19101 0.0 0.2 5160 828 pts/0 S+ 13:32 0:00 grep user<br />
</pre><br />
<br />
$ ps aux | grep ${USER} | awk '{print $2 " " $11}' | sort -gr <br />
19105 sort<br />
19104 awk<br />
19103 grep<br />
19102 ps<br />
'''19090 -bash'''<br />
19089 sshd:<br />
19087 sshd:<br />
<br />
-awk option.<br />
<pre><br />
#print the 2nd and the 11th columns<br />
print $2 " " $11<br />
<br />
(sort options<br />
-g, --general-numeric-sort compare according to general numerical value<br />
-r, --reverse reverse the result of comparisons)<br />
</pre><br />
<br />
-Kill the bash<br />
<pre><br />
$ sudo kill -s 9 19090 <br />
</pre><br />
<br />
<br />
===To Kick out Another Login of Yours===<br />
$ who <br />
user pts/0 2008-08-24 13:25 <br />
'''user pts/1 2008-08-24 12:50 ''' <= to kick this out.<br />
<br />
$ ps a <br />
PID TTY STAT TIME COMMAND<br />
2552 tty1 Ss+ 0:00 /sbin/getty 38400 tty1<br />
'''18988 pts/1 Ss+ 0:00 -bash'''<br />
19044 pts/0 Ss 0:00 -bash<br />
19078 pts/0 R+ 0:00 ps a<br />
<br />
$ kill -s 9 18988 <br />
<br />
$ who <br />
user pts/0 2008-08-24 13:25 <br />
<br />
$ ps a <br />
PID TTY STAT TIME COMMAND<br />
2552 tty1 Ss+ 0:00 /sbin/getty 38400 tty1<br />
19044 pts/0 Ss 0:00 -bash<br />
19080 pts/0 R+ 0:00 ps a</div>Kevinhttp://kevinlee.io/wiki/Linux_Software_PackageLinux Software Package2015-06-30T13:39:38Z<p>Kevin: </p>
<hr />
<div>[[Category:Linux]]<br />
== Check Package ==<br />
<pre><br />
$ dpkg -l | grep <package name><br />
</pre><br />
<pre><br />
$ apt-cache policy <package name><br />
</pre><br />
<pre><br />
$ apt-cache search <package name in regular expression><br />
</pre><br />
<br />
== Lock Version ==<br />
* Lock the current version of a package (so it will not be upgraded)<br />
<br />
<pre><br />
sudo apt-mark hold package_name <br />
</pre><br />
<br />
* Unlock the locked package.<br />
<pre><br />
sudo apt-mark unhold package_name <br />
</pre><br />
<br />
<br />
== Troubleshooting ==<br />
After run <code>apt-get update</code> if there is a GPG error like<br />
<pre><br />
W: GPG error: http://ppa.launchpad.net lucid Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 608BF7B93528AE20<br />
</pre><br />
<br />
Take the last 8 characters from the keyid<br />
<pre><br />
e.g.)<br />
from the keyid<br />
608BF7B93528AE20<br />
take 3528AE20<br />
</pre><br />
Run the following commands<br />
gpg --keyserver keyserver.ubuntu.com --recv 3528AE20<br />
gpg --export --armor 3528AE20 | sudo apt-key add -<br />
<br />
OR<br />
Use the full keyid and put the 0x before it then run the following commands<br />
wget -q "http://keyserver.ubuntu.com:11371/pks/lookup?op=get&search=0x608BF7B93528AE20" -O- | sudo apt-key add -<br />
If it does not work, remove the port number and try again.<br />
wget -q "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x608BF7B93528AE20" -O- | sudo apt-key add -<br />
<br />
To see all the keys,<br />
sudo apt-key list</div>Kevinhttp://kevinlee.io/wiki/PostgreSQLPostgreSQL2015-05-20T13:25:19Z<p>Kevin: /* Installation */</p>
<hr />
<div>[[Category:Linux]]<br />
[[Category:Software Development]]<br />
[[Category:Database]]<br />
{{Infobox software<br />
| name = PostgreSQL<br />
| logo = <br />
| screenshot =<br />
| caption =<br />
| developer = PostgreSQL Global Development Group<br />
| latest release version = 9.2.2,<br />9.1.7,<br />9.0.11,<br />8.4.15,<br />8.3.22<br />
| latest release date = <br />{{Start date and age|2012|12|06}}<br />
| latest preview version = <!--Add alpha, when it becomes available--><br />
| latest preview date = <!--{{Start date and age|YYYY|MM|DD}}--><br />
| operating system = [[Cross-platform]]<br />
| programming language = [[C (programming language)|C]]<br />
| genre = [[Object-relational database|ORDBMS]]<br />
| license = PostgreSQL licence<ref>{{cite web |date=2010-02-18|title=PostgreSQL licence approved by OSI| url= http://www.crynwr.com/cgi-bin/ezmlm-cgi?17:mmp:969| publisher = Crynwr | accessdate = 2010-02-18}}</ref><ref>{{cite web | publisher = Open Source Initiative | date = 2010-02-20|title=OSI PostgreSQL licence|url=http://www.opensource.org/licenses/postgresql | accessdate = 2010-02-20}}</ref><ref>{{cite web|url= http://www.postgresql.org/about/licence|title= License| publisher = PostgreSQL Global Development Group|accessdate=2010-09-20}}</ref><br />
| website = {{URL|www.postgresql.org}}<br />
}}<br />
<br />
==PostgreSQL==<br />
[http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] is an object-relational database management system (ORDBMS). The documentation of PostgreSQL 8.4.4 can be found [http://www.postgresql.org/docs/8.4/static here]<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static | title = PostgreSQL 8.4.4 Documentation | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
==Installation==<br />
*Install PostgreSQL<br />
<pre><br />
$ sudo apt-get install postgresql <br />
</pre><br />
<br />
=== For Latest Version ===<br />
<pre><br />
vim /etc/apt/sources.list.d/pgdg.list <br />
</pre><br />
<br />
add<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ YOUR_UBUNTU_VERSION_HERE-pgdg main <br />
</pre><br />
e.g.) 14.04<br />
<pre><br />
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main <br />
</pre><br />
<br />
Then import the repository signing key.<br />
<pre><br />
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \<br />
sudo apt-key add - <br />
</pre><br />
<br />
Now update and install.<br />
<pre><br />
apt-get update <br />
apt-get install postgresql <br />
</pre><br />
<br />
* Reference: http://www.postgresql.org/download/linux/ubuntu/<br />
<br />
==Administration==<br />
=== Setup ===<br />
====Setup Authentication Methods====<br />
* In order to allow new users to log in the PostgreSQL server, <code>/etc/postgresql/8.4/main/pg_hba.conf</code> should be modified.<br />
<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all ident<br />
# IPv4 local connections:<br />
host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
host all all ::1/128 md5<br />
To<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all md5<br />
# IPv4 local connections:<br />
#host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
#host all all ::1/128 md5<br />
Allowing the local access only with username & password.<br />
<br />
<br />
==== Access Remotely ====<br />
* Edit <code>/etc/postgresql/9.1/main/postgresql.conf</code><br />
<pre><br />
listen_addresses = 'localhost,address,separated,by,comma'<br />
</pre><br />
'''The IP is the local one but NOT a remote one'''.<br />
e.g.) 192.168.0.111 means it listens any access to 192.168.0.111 (local IP).<br />
<br />
e.g.)<br />
<pre><br />
listen_addresses = 'localhost,192.168.0.111'<br />
</pre><br />
<br />
* Edit <code>/etc/postgresql/9.1/main/pg_hba.conf</code><br />
<pre><br />
host all all IP/HERE trust<br />
</pre><br />
e.g.)<br />
<pre><br />
host all all 192.168.0.121/32 trust<br />
</pre><br />
'''The IP is not the local one but a remote one'''.<br />
e.g.) 192.168.0.121 means access from 192.168.0.121 is allowed.<br />
<br />
* Test if it works.<br />
<pre><br />
# -W: force password prompt<br />
$ psql -h IP-TO-DB-SERVER -U USERNAME -W <br />
</pre><br />
e.g.)<br />
<pre><br />
$ psql -h 192.168.0.123 -U db_user -W <br />
</pre><br />
<br />
===Roles (Users)===<br />
====Setup Root User Password====<br />
*Set up the password of the PostgreSQL root user<br />
$ sudo -u postgres psql postgres <br />
<br />
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.<br />
<br />
Type: \copyright for distribution terms<br />
\h for help with SQL commands<br />
\? for help with psql commands<br />
\g or terminate with semicolon to execute query<br />
\q to quit<br />
<br />
postgres=# '''ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';''' <br />
or<br />
postgres=# '''\password postgres''' <br />
<br />
Use <code>\quit</code> or <code>\q</code> to quit psql.<br />
postgres=# \q <br />
<br />
<br />
====Create Role (User)====<br />
Creating a user in PostgreSQL is in fact creating a role. So there are two ways to do it. The first one is using <code>CREATE ROLE</code><ref>{{cite web |url=http://www.postgresql.org/docs/8.4/static/database-roles.html | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles | date = 2009 | accessdate = 2010-08-02}}</ref> SQL command and the other one is using the programme, <code>createuser</code><ref>{{cite web|url= http://www.postgresql.org/docs/8.4/static/app-createuser.html | title= PostgreSQL 8.4.4 Documentation - createuser | date = 2009 | accessdate = 2010-08-02}}</ref>.<br />
<br />
=====CREATE ROLE=====<br />
<code>CREATE ROLE</code><ref>{{cite web |url = http://www.postgresql.org/docs/8.4/static/sql-createrole.html | title = PostgreSQL 8.4.4 Documentation - CREATE ROLE | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
CREATE ROLE role_name; <br />
CREATE ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
CREATE USER role_name WITH ENCRYPTED PASSWORD 'password'; <br />
(CREATE USER == CREATE ROLE + LOGIN) <br />
<br />
'''Usually use this.'''<br />
CREATE ROLE "role_name" WITH LOGIN ENCRYPTED PASSWORD 'password';<br />
<br />
=====createuser=====<br />
createuser [option...] [username] <br />
<br />
So to create a new user and a db belonging to him/her.<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-P: it will ask you to enter the password for the new user ('''role''').<br />
<br />
<br />
* Options<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createuser.html#AEN67555 | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles#Options | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
createuser accepts the following command-line arguments:<br />
<br />
username<br />
<br />
Specifies the name of the PostgreSQL user to be created. This name must be different from all existing roles in this PostgreSQL installation. <br />
-s<br />
--superuser<br />
<br />
The new user will be a superuser. <br />
-S<br />
--no-superuser<br />
<br />
The new user will not be a superuser. This is the default. <br />
-d<br />
--createdb<br />
<br />
The new user will be allowed to create databases. <br />
-D<br />
--no-createdb<br />
<br />
The new user will not be allowed to create databases. This is the default. <br />
-r<br />
--createrole<br />
<br />
The new user will be allowed to create new roles (that is, this user will have CREATEROLE privilege). <br />
-R<br />
--no-createrole<br />
<br />
The new user will not be allowed to create new roles. This is the default. <br />
-l<br />
--login<br />
<br />
The new user will be allowed to log in (that is, the user name can be used as the initial session user identifier). This is the default. <br />
-L<br />
--no-login<br />
<br />
The new user will not be allowed to log in. (A role without login privilege is still useful as a means of managing database permissions.) <br />
-i<br />
--inherit<br />
<br />
The new role will automatically inherit privileges of roles it is a member of. This is the default. <br />
-I<br />
--no-inherit<br />
<br />
The new role will not automatically inherit privileges of roles it is a member of. <br />
-c number<br />
--connection-limit number<br />
<br />
Set a maximum number of connections for the new user. The default is to set no limit. <br />
-P<br />
--pwprompt<br />
<br />
If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication. <br />
-E<br />
--encrypted<br />
<br />
Encrypts the user's password stored in the database. If not specified, the default password behavior is used. <br />
-N<br />
--unencrypted<br />
<br />
Does not encrypt the user's password stored in the database. If not specified, the default password behavior is used. <br />
-e<br />
--echo<br />
<br />
Echo the commands that createuser generates and sends to the server. <br />
<br />
You will be prompted for a name and other missing information if it is not specified on the command line.<br />
<br />
createuser also accepts the following command-line arguments for connection parameters:<br />
<br />
-h host<br />
--host host<br />
<br />
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. <br />
-p port<br />
--port port<br />
<br />
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. <br />
-U username<br />
--username username<br />
<br />
User name to connect as (not the user name to create). <br />
-w<br />
--no-password<br />
<br />
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. <br />
-W<br />
--password<br />
<br />
Force createuser to prompt for a password (for connecting to the server, not for the password of the new user).<br />
<br />
This option is never essential, since createuser will automatically prompt for a password if the server demands password authentication. However, createuser will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt. <br />
<br />
<br />
====View Roles====<br />
<br />
* To see all the exising roles.<br />
SELECT * FROM pg_roles;<br />
<br />
* To see the details of an existing role<br />
SELECT role_name FROM pg_roles; <br />
<br />
<br />
====Change Role====<br />
* <code>ALTER ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-alterrole.html | title = PostgreSQL 8.4.4 Documentation - ALTER ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
ALTER ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
<br />
<br />
====Remove Role====<br />
* Using <code>DROP ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-droprole.html | title = PostgreSQL 8.4.4 Documentation - DROP ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
DROP ROLE role_name; <br />
<br />
*Using <code>dropuser</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-dropuser.html | title = PostgreSQL 8.4.4 Documentation - dropuser | date = 2009 | accessdate = 2010-08-02}}</ref><br />
dropuser name <br />
<br />
===Databases===<br />
====List Databases====<br />
postgres=# \l <br />
<br />
====Select (Change) Database====<br />
postgres=# \c DB_NAME <br />
<br />
====List Table====<br />
<br />
postgres=# \d <br />
<br />
====Display Table Description====<br />
<br />
postgres=# \d TABLE_NAME <br />
<br />
<br />
====Creating Database====<br />
* Using <code>CREATE DATABASE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-createdatabase.html | title = PostgreSQL 8.4.4 Documentation - CREATE DATABASE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL to create a new database.<br />
CREATE DATABASE "db_name" <br />
<br />
CREATE DATABASE "db_name" OWNER "role_name" <br />
<br />
* Similar to creating a role, it is also possible to create a new database from the shell by using <code>createdb</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createdb.html | title = PostgreSQL 8.4.4 Documentation - createdb | date = 2009 | accessdate = 2010-08-02}}</ref> command.<br />
createdb [option...] [db_name] [description]<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-O owner<br />
--owner owner<br />
Specifies the database user who will own the new database.<br />
<br />
====Change Database====<br />
e.g.)<br />
postgres=# ALTER DATABASE db_name OWNER TO new_owner; <br />
<br />
====Privileges====<br />
=====GRANT=====<br />
e.g.)<br />
postgres=# GRANT ALL PRIVILEGES ON DATABASE db_name TO username; <br />
<br />
<br />
====Drop and Recreate database====<br />
* Access db<br />
$ sudo -u postgres psql postgres <br />
And drop and recreate the database.<br />
DROP DATABASE DB_NAME; <br />
CREATE DATABASE DB_NAME OWNER ROLE_NAME; <br />
<br />
====Back Up Database====<br />
$ sudo -u postgres pg_dump DB_NAME > DB_DUMP_DB_NAME.sql <br />
<br />
====Restore Database====<br />
$ sudo -u postgres psql --set ON_ERROR_STOP=on DB_NAME < DB_DUMP_DB_NAME.sql <br />
<br />
<br />
====Run SQL Script====<br />
* Use psql terminal and select DB.<br />
$ sudo -u postgres psql <br />
postgres=# \c DB_NAME <br />
OR Access the DB directly<br />
$ sudo -u postgres psql DB_NAME <br />
DB_NAME=# <br />
<br />
* Run SQL script from within psql<br />
DB_NAME=# \set ON_ERROR_STOP <br />
DB_NAME=# \i db.sql<br />
<br />
* Run SQL query<br />
DB_NAME=# type SQL; <br />
e.g.)<br />
DB_NAME=# SELECT * FROM some_table;<br />
<br />
====Store the Query Result in File====<br />
DB_NAME=# \o file_name <br />
# Run some query<br />
DB_NAME=# SELECT * FROM some_table; <br />
# \o to finish<br />
DB_NAME=# \o<br />
==== Store Query Result in CSV file ====<br />
<pre><br />
\copy (SELECT * FROM products WHERE product_name LIKE 'SomePreFix%') To '/path/to/store/csv-file.csv' With CSV HEADER; <br />
</pre><br />
<br />
====Terminate any processing user of database====<br />
* To terminate all the processing users of the database, run the following script.<br />
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='DB_NAME';<br />
<br />
<br />
<br />
===Restart Server===<br />
* To restart the server, /etc/init.d/postgresql-8.x (depending on the version).<br />
sudo /etc/init.d/postgresql-8.4 restart<br />
<br />
==Install pgAdmin III==<br />
<pre><br />
$ sudo apt-get install pgadmin3 <br />
</pre><br />
<br />
==Troubleshooting==<br />
===Shared Memory Segment Issue===<br />
* http://askubuntu.com/questions/44373/how-to-fix-postgresql-installation<br />
* http://leopard.in.ua/2013/09/05/postgresql-sessting-shared-memory<br />
<br />
==References==<br />
{{Reflist|1}}</div>Kevinhttp://kevinlee.io/wiki/AnsibleAnsible2015-05-20T12:59:55Z<p>Kevin: Created page with "= Installation = <pre> sudo apt-get install software-properties-common sudo apt-add-repository ppa:ansible/ansible sudo apt-get update sudo apt-get install ansible </pre>"</p>
<hr />
<div>= Installation =<br />
<pre><br />
sudo apt-get install software-properties-common <br />
sudo apt-add-repository ppa:ansible/ansible <br />
sudo apt-get update <br />
sudo apt-get install ansible <br />
</pre></div>Kevinhttp://kevinlee.io/wiki/WkhtmltopdfWkhtmltopdf2015-04-12T03:35:06Z<p>Kevin: </p>
<hr />
<div>[[Category:Linux]]<br />
<br />
= Installation =<br />
<br />
* Download the latest wkhtmltopdf from [http://wkhtmltopdf.org/downloads.html its download page]. <del>http://code.google.com/p/wkhtmltopdf/downloads/list</del><br />
* <del>Extract the file and copy to /usr/bin</del><br />
* It provides installation package for each well-known OS so just use the package for your OS to install.<br />
<br />
e.g.)<br />
* For Ubuntu<br />
<pre><br />
dpkg -i wkhtmltox-0.12.2.1_linux-trusty-amd64.deb <br />
</pre><br />
You can also get it from the Ubuntu software repository but that version might not be the latest one.<br />
<pre><br />
apt-get install wkhtmltopdf <br />
</pre><br />
<br />
* For Mac OS X, use [http://brew.sh/ Homebrew] [http://caskroom.io/ Cask] (You don't need to download the wkhtmltopdf package from the website).<br />
<pre><br />
brew cask install wkhtmltopdf <br />
</pre><br />
<br />
== '''The following instructions are outdated.''' ==<br />
e.g.)<br />
<pre><br />
$ tar -jxvf wkhtmltopdf-0.11.0_rc1-static-i386.tar.bz2 <br />
<br />
$ mv wkhtmltopdf-i386 wkhtmltopdf-i386-0.11.0_rc1 <br />
<br />
$ chown root:root wkhtmltopdf-i386-0.11.0_rc1 <br />
<br />
$ mv wkhtmltopdf-i386-0.11.0_rc1 /usr/bin/ <br />
</pre><br />
<br />
* Create <code>/usr/bin/wkhtmltopdf.sh</code><br />
<pre><br />
#!/bin/bash<br />
<br />
xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf-i386-0.11.0_rc1 $*<br />
<br />
</pre><br />
<br />
<pre><br />
$ chmod a+x /usr/bin/wkhtmltopdf.sh <br />
<br />
$ cd /usr/bin <br />
<br />
$ ln -s wkhtmltopdf.sh wkhtmltopdf <br />
</pre><br />
<br />
If running wkhtmltopdf causes the following error<br />
<pre><br />
wkhtmltopdf: cannot connect to X server <br />
</pre><br />
Or<br />
<pre><br />
/usr/bin/wkhtmltopdf: line 4: xvfb-run: command not found<br />
</pre><br />
<br />
Install xvfb<br />
<pre><br />
$ apt-get install xvfb <br />
</pre></div>Kevinhttp://kevinlee.io/wiki/GitGit2015-03-31T10:43:36Z<p>Kevin: /* Usage */</p>
<hr />
<div>[[Category:Revision Control]]<br />
= Git =<br />
== Client-side Installation ==<br />
<br />
In the client,<br />
$ apt-get install git-core <br />
<br />
Or to install the latest version of Git on Ubuntu<br />
add-apt-repository ppa:git-core/ppa <br />
apt-get update <br />
apt-get install git-core <br />
<br />
== Configuration ==<br />
=== User Info ===<br />
$ git config --global user.name "GivenName Surname" <br />
$ git config --global user.email user@email.address <br />
<br />
=== Default Text Editor ===<br />
$ git config --global core.editor "sublime-text -w"<br />
<br />
=== External Diff Tool ===<br />
==== Meld As External Diff Tool ====<br />
* To use Meld as a diff tool, install Meld<br />
$ sudo apt-get install meld <br />
* Set it as the default diff tool<br />
$ git config --global diff.external meld <br />
<br />
* It doesn't work and gives the following error messages.<br />
<pre><br />
$ git diff <br />
Usage: <br />
meld Start with an empty window<br />
meld <file|dir> Start a version control comparison<br />
meld <file> <file> [<file>] Start a 2- or 3-way file comparison<br />
meld <dir> <dir> [<dir>] Start a 2- or 3-way directory comparison<br />
meld <file> <dir> Start a comparison between file and dir/file<br />
<br />
meld: error: too many arguments (wanted 0-4, got 7)<br />
</pre><br />
<br />
* Create ~/diff.py<br />
<pre><br />
#!/usr/bin/python<br />
<br />
import sys<br />
import os<br />
<br />
os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))<br />
</pre><br />
* Make it executable<br />
$ chmod a+x ~/diff.py <br />
* Set the script as the diff tool<br />
$ git config --global diff.external ~/diff.py <br />
* Now it works<br />
$ git diff <br />
$ git diff HEAD <br />
<br />
<br />
==== Meld as Diff Tool ====<br />
* This way is much easier.<br />
$ git config --global diff.tool meld <br />
<br />
$ git difftool<br />
<br />
==== Meld as Diff Tool ====<br />
$ git config --global diff.tool vimdiff <br />
<br />
<br />
=== Unset External Diff Tool ===<br />
$ git config --global --unset diff.external <br />
<br />
<br />
=== Commit Template ===<br />
Create a template file<br/><br />
e.g.) $HOME/git/.your-config/git-commit-comment-template.txt<br />
CLOSED - #TICKET_NUMBER: Summary<br />
TICKET_URI<br />
Details<br />
<br />
$ git config --global commit.template $HOME/git/.your-config/git-commit-comment-template.txt <br />
<br />
<br />
=== Etc. ===<br />
$ git config --global color.ui true<br />
<br />
== Server-side Installation ==<br />
In the server,<br />
$ apt-get install git <br />
<br />
= Gitolite =<br />
== Add User to Manage Gitolite ==<br />
# Add user to manage gitolite <br />
<br />
$ adduser \<br />
--system \<br />
--shell /bin/bash \<br />
--gecos 'git version control' \<br />
--group \<br />
--disabled-password \<br />
--home /home/git \<br />
git<br />
<br />
Adding system user `git' (UID 108) ...<br />
Adding new group `git' (GID 110) ...<br />
Adding new user `git' (UID 108) with group `git' ...<br />
Creating home directory `/home/git' ...<br />
<br />
== Create SSH key ==<br />
$ ssh-keygen -t rsa <br />
/home/username/.ssh/some_git_rsa<br />
[ENTER]<br />
[ENTER]<br />
<br />
== Transfer SSH Keys to Server ==<br />
$ ssh-copy-id '-p PORT_NUMBER -i /home/user/.ssh/some_git_rsa.pub git_user@host' <br />
If it doesn't work because the user is the only user who can access the server with his/her ssh key so accessing with git_user doesn't work.<br />
<br />
$ scp -P PORT ~/.ssh/some_git_rsa.pub user@host: <br />
<br />
Log in with the user account<br />
$ ssh -p PORT user@host<br />
$ mv some_git_rsa.pub git.pub <br />
<br />
== Setup ==<br />
$ su - git <br />
$ cd ~ <br />
$ echo "PATH=$HOME/bin:$PATH" >> ~/.bashrc <br />
<br />
* don't need these 2 lines<br />
$ mkdir .ssh <br />
$ cat /home/user/git.pub >> /home/git/.ssh/authorized_keys <br />
<br />
$ exit<br />
<br />
== Gitolite Installation ==<br />
$ sudo -i <br />
$ apt-get install gitolite<br />
[ENTER] -> :q to exit<br />
$ exit<br />
<br />
== Setup git User ==<br />
$ su - git<br />
$ gl-setup /home/user/git.pub <br />
<br />
$ vim /etc/ssh/sshd_config <br />
Add git to the allowed user<br />
<br />
AllowUsers user git<br />
<br />
Restart ssh<br />
$ service ssh restart <br />
<br />
== Test ==<br />
* Testing (from user local machine)<br />
$ ssh -p PORT_NUMBER git@host <br />
Having many keys?<br />
$ ssh -p PORT_NUMBER -i ~/.ssh/user@host1.pub git@host <br />
<br />
For debugging<br />
$ ssh -p PORT_NUMBER -vT git@host <br />
<br />
$ mkdir some-dir <br />
$ cd some-dir <br />
$ git clone git@host:gitolite-admin <br />
Or if you use a specific port,<br />
$ git clone ssh://git@host:PORT_NUMBER/gitolite-admin<br />
<br />
== Add Git User (through Gitolite) ==<br />
Adding User<br />
$ cd gitolite-admin/keydir <br />
$ mkdir user_name <br />
$ cp -p ~/.ssh/user@host1.pub .<br />
$ cp -p ~/.ssh/user@host2.pub .<br />
$ git add user@host1.pub <br />
$ git commit -a -m "users added: user@host1 and user@host2" <br />
$ git push<br />
<br />
If directly copied to <code>/home/git/.gitolite/keydir</code>, run<br />
$ gl-setup<br />
to apply the changes<br />
<br />
= Usage =<br />
== Initializing a Repository in an Existing Directory ==<br />
* To make a directory a git repository, go to the project directory and run the following command<br />
<pre><br />
$ git init <br />
</pre><br />
Then it will create a new directory named <code>.git</code> inside the project directory. The <code>.git</code> folder contains all necessary files for the git repository.<br />
<br />
== Host Info Config using SSH_Config ==<br />
Create <code>~/.ssh/config</code> file and edit.<br />
<br />
Host host.name<br />
Hostname host.name<br />
Port 1111<br />
<br />
Host another.host.name<br />
Hostname another.host.name<br />
Port 2222<br />
<br />
Host host1<br />
Hostname host.name<br />
Port 1111<br />
User git<br />
IdentityFile ~/.ssh/git_rsa<br />
<br />
Host user-host2<br />
Hostname host2.name<br />
Port 1234<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
Host user-host3<br />
Hostname host3.com<br />
Port 1122<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
'''IdentityFile is a private key'''<br />
<br />
To use this info,<br />
$ git clone git@host1:repo-name <br />
# it will use 'git' as a user and access the server 'host.name' using the port nubmer 1111.<br />
<br />
$ git clone git@host1:repo-name <br />
# it will use 'user' as a user and access the server 'host2.name' using the port nubmer 1234.<br />
<br />
== Push to Specific Host ==<br />
<br />
$ git push git@host1:repo-name master<br />
<br />
== Set up Project Specific User Info ==<br />
* Move to the project repository<br />
$ git config user.email user@email.address <br />
<br />
To see and edit all the config details<br />
$ git config -e<br />
<br />
== .gitignore ==<br />
To exclude files when committing, create .gitignore file and put in the project root (git project repo root).<br />
For maven project, the content should be<br />
target/<br />
<br />
To set up the global ignore file<br />
$ git config --global core.excludesfile $HOME/git/.your-config/.global-gitignore <br />
<br />
Other gitignore files,<br />
https://github.com/github/gitignore<br />
<br />
<br />
=== .gitignore for Eclipse and Maven (for Kevin) ===<br />
<pre><br />
target/<br />
<br />
*.pydevproject<br />
.project<br />
.classpath<br />
.settings/<br />
.metadata<br />
bin/<br />
tmp/<br />
*.tmp<br />
*.bak<br />
*.swp<br />
*~.nib<br />
local.properties<br />
.loadpath<br />
<br />
# External tool builders<br />
.externalToolBuilders/<br />
<br />
# Locally stored "Eclipse launch configurations"<br />
*.launch<br />
<br />
# CDT-specific<br />
.cproject<br />
<br />
# PDT-specific<br />
.buildpath<br />
</pre><br />
<br />
<br />
== Commit ==<br />
* Add a file<br />
$ git add fileName<br />
<br />
* Commit changes<br />
$ git commit -m "comment"<br />
<br />
* Add all the files and folders<br />
$ git add -A <br />
# It is equivalent to git add .; git add -u<br />
# git add . -> add all changed and new ones but not removed ones<br />
# git add -u -> changed and removed but not new ones.<br />
<br />
=== Amend Last Comment ===<br />
* Amend the last comment<br />
$ git commit --amend -m "New comment"<br />
<br />
== Push ==<br />
=== Add Remote ===<br />
* To view all remote<br />
<pre><br />
$ git remote -v <br />
</pre><br />
<br />
* Add a remote<br />
<pre><br />
$ git remote add origin git@git-repo:project <br />
</pre><br />
Then<br />
<pre><br />
$ git push -u origin master <br />
</pre><br />
Or<br />
<pre><br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
</pre><br />
=== Push to Remote ===<br />
* Push to the remote git repository<br />
$ git push<br />
<br />
* If it gives error messages like:<br />
No refs in common and none specified; doing nothing.<br />
Perhaps you should specify a branch such as 'master'.<br />
(It doesn't happen if you have already done <code>git push -u origin master </code>).<br />
* Do<br />
$ git push origin master<br />
<br />
== Revert ==<br />
=== Reset ===<br />
* Revert to last commit.<br />
$ git reset --hard HEAD <br />
Or to specific commit/tag/branch<br />
$ git reset --hard <tag/branch/commit id> <br />
<br />
* Check the id using git log<br />
$ git log <br />
<br />
* To push it<br />
$ git push <reponame> -f <br />
<br />
=== Check out ===<br />
* Or change the name of master branch then check out from the commit to be used and make it master<br />
<br />
# Rename the current master branch<br />
#: <pre>$ git branch -m another_name_for_master </pre> <br />
# Check out from another commit:<br />
#: <pre> $ git checkout a1b2c3a3blah </pre><br />
# Make it the new master branch:<br />
#: <pre> $ git checkout -b master </pre><br />
* If the old master is no longer needed and has to be removed, remove it by<br />
$ git branch -D another_name_for_master<br />
<br />
<br />
=== Restore Removed File ===<br />
'''This has to be tested first then should be rewritten'''.<br /><br />
Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.<br />
$ git rev-list -n 1 HEAD -- <file_path> <br />
<br />
Then checkout the version at the commit before.<br />
$ git checkout <deleting_commit>^ -- <file_path> <br />
<br />
Or in one command, if $file is the file.<br />
$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"<br />
* Reference<br />
[http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top]<br />
<br />
<br />
== Branch ==<br />
=== Create Branch ===<br />
* Create branch<br />
<pre><br />
$ git branch branch_name <br />
</pre><br />
* Change to the branch just created<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
OR<br />
* Create and change<br />
<pre><br />
git checkout -b branch_name <br />
</pre><br />
<br />
* Back to master<br />
<pre><br />
$ git checkout master <br />
</pre><br />
<br />
* To push,<br />
<pre><br />
$ git push origin branch_name <br />
</pre><br />
<br />
=== Change Branch ===<br />
* Change branch<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
<br />
* It does not work when the local git repo has multiple remote repositories. If it's the case,<br />
<pre><br />
$ git checkout -b branch remote/branch <br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ git checkout -b my-branch origin/my-branch <br />
</pre><br />
<br />
<br />
=== Pull Without Checkout ===<br />
* To pull a specific branch without checking out<br />
<pre><br />
$ git fetch <remote> <remoteBranch>:<localBranch> <br />
</pre><br />
e.g.)<br />
<pre><br />
git fetch origin master:master <br />
</pre><br />
<pre><br />
git fetch origin adding-new-feature:adding-new-feature <br />
</pre><br />
<br />
== Tag ==<br />
=== List Tags ===<br />
<pre><br />
$ git tag <br />
</pre><br />
<br />
=== View Tag ===<br />
* To view tag data with commit details.<br />
<pre><br />
$ git show tag_name <br />
</pre><br />
<br />
=== Create Tag ===<br />
* Create Tag<br />
<pre><br />
$ git tag -a tag_name -m 'tag message: new version blah blah' <br />
</pre><br />
* Tag later <br />
<pre><br />
$ git tag -a tag_name -m 'tag message' commit_checksum <br />
</pre><br />
e.g.)<br />
<pre><br />
$ git tag -a v1.5 -m 'new version with blah blah' b015e9d <br />
</pre><br />
* To see the list of checksum use the following command.<br />
<pre><br />
$ git log --pretty=oneline <br />
</pre><br />
<br />
=== Remove Tag ===<br />
<pre><br />
$ git tag -d tag_name <br />
</pre><br />
<br />
<br />
== Merge Multiple Git Repositories into One ==<br />
If the directory structures look like this<br />
<pre><br />
parent-dir (not git repo)<br />
│<br />
├─git-repo1 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
├─git-repo2 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
└─ git-repo3 ## each is connected to its own remote git repository<br />
├─src<br />
└.git<br />
</pre><br />
<br />
To make it like<br />
<pre><br />
parent-dir (now it is git repo)<br />
├─src ## contains all the files and sub-dirs from those three repositories (git-repo1, git-repo2 and git-repo3).<br />
└.git<br />
<br />
</pre><br />
and it still keeps all the histories from all those repositories, do following instructions.<br />
<br />
* Initialize the new repository directory.<br />
<pre><br />
$ cd parent-dir <br />
$ git init <br />
</pre><br />
<br />
* Pull the old ones and remove these one by one.<br />
<pre><br />
$ git pull git-repo1 <br />
<br />
## If there is any conflict, solve it first then<br />
## commit the changes<br />
$ git add file <br />
$ git commit -a <br />
## Now pull the one had conflict issue again.<br />
$ git pull git-repo1 <br />
<br />
$ rm -rf git-repo1 <br />
<br />
$ git pull git-repo2 <br />
$ rm -rf git-repo2 <br />
<br />
$ git pull git-repo3 <br />
$ rm -rf git-repo3 <br />
</pre><br />
<br />
* Done! Add the remote repo info if necessary.<br />
<br />
<br />
== Multiple Remote Repositories ==<br />
<br />
=== Push ===<br />
==== Add more remote repository (origin) for <code>push</code> ====<br />
<pre><br />
git remote set-url origin --add git@github.com:YOUR_USERNAME/your_project.git <br />
</pre><br />
<br />
=== Pull ===<br />
==== Add remote repository info for <code>pull</code> ====<br />
<pre><br />
git remote add github git@github.com:YOUR_USERNAME/YOUR_PROJECT.git <br />
git remote add gitlab git@gitlab.com:YOUR_USERNAME/YOUR_PROJECT.git <br />
git remote add bitbucket git@bitbucket.org:YOUR_USERNAME/your_project.git <br />
</pre><br />
<br />
==== Pull from all remote repos ====<br />
* For bash<br />
<pre><br />
gitpullall()<br />
{<br />
repos_to_ignore="origin"<br />
should_ignore=-1<br />
<br />
echo "running: git pull"<br />
git pull<br />
echo ""<br />
<br />
for remote in `git remote`<br />
do<br />
echo "=================================="<br />
echo "Remote: $remote"<br />
echo "----------------------------------"<br />
for ignore in $repos_to_ignore<br />
do<br />
[ "$remote" == "$ignore" ] && { should_ignore=1; break; } || :<br />
done<br />
<br />
if [ "$should_ignore" == "-1" ]<br />
then<br />
THIS_BRANCH_NAME=`git rev-parse --abbrev-ref HEAD`<br />
git branch -r | grep -s "^[[:space:]]\+$remote/$THIS_BRANCH_NAME$" 2>&1 > /dev/null || git branch -r | grep -s "^[[:space:]]\+$remote/$THIS_BRANCH_NAME""[[:space:]]\+" 2>&1 > /dev/null || {<br />
echo "No info of the remote repo named '$remote' is found."<br />
echo "So $remote will be fetched."<br />
echo "running: git fetch $remote"<br />
git fetch "$remote"<br />
echo ""<br />
}<br />
if git branch -r | grep -sw "$remote/$THIS_BRANCH_NAME" 2>&1 > /dev/null ; then<br />
echo "running: git pull $remote $THIS_BRANCH_NAME"<br />
git pull "$remote" "$THIS_BRANCH_NAME"<br />
else<br />
echo "$THIS_BRANCH_NAME branch does not exist on the remote called '$remote'."<br />
echo "So $remote will not be pulled from $remote."<br />
fi<br />
else<br />
echo "$remote is on the ignored list so ignore it."<br />
echo "Ignored: $remote"<br />
fi<br />
should_ignore=-1<br />
echo "=================================="<br />
echo ""<br />
done<br />
}<br />
</pre><br />
* For ZSH<br />
<pre><br />
gitpullall()<br />
{<br />
repos_to_ignore="origin"<br />
should_ignore=-1<br />
<br />
echo "running: git pull"<br />
git pull<br />
echo ""<br />
<br />
for remote in `git remote`<br />
do<br />
echo "=================================="<br />
echo "Remote: $remote"<br />
echo "----------------------------------"<br />
for ignore in $repos_to_ignore<br />
do<br />
[ "$remote" = "$ignore" ] && { should_ignore=1; break; } || :<br />
done<br />
<br />
if [ "$should_ignore" = "-1" ]<br />
then<br />
THIS_BRANCH_NAME=`git rev-parse --abbrev-ref HEAD`<br />
{ git branch -r | grep -s "^[[:space:]]\+$remote/$THIS_BRANCH_NAME$" 2>&1 > /dev/null } || { git branch -r | grep -s "^[[:space:]]\+$remote/$THIS_BRANCH_NAME""[[:space:]]\+" 2>&1 > /dev/null } || {<br />
echo "No info of the remote repo named '$remote' is found."<br />
echo "So $remote will be fetched."<br />
echo "running: git fetch $remote"<br />
git fetch "$remote"<br />
echo ""<br />
}<br />
if git branch -r | grep -sw "$remote/$THIS_BRANCH_NAME" 2>&1 > /dev/null ; then<br />
echo "running: git pull $remote $THIS_BRANCH_NAME"<br />
git pull "$remote" "$THIS_BRANCH_NAME"<br />
else<br />
echo "$THIS_BRANCH_NAME branch does not exist on the remote called '$remote'."<br />
echo "So $remote will not be pulled from $remote."<br />
fi<br />
else<br />
echo "$remote is on the ignored list so ignore it."<br />
echo "Ignored: $remote"<br />
fi<br />
should_ignore=-1<br />
echo "=================================="<br />
echo ""<br />
done<br />
}<br />
</pre><br />
<br />
=== Checkout ===<br />
==== Checkout to a branch for git repo having multiple remote repos. ====<br />
<pre><br />
git checkout -b existing-branch origin/existing-branch <br />
</pre><br />
<br />
= Migration from SVN to Git =<br />
== Install git-svn ==<br />
$ apt-get install git-svn <br />
<br />
== Prepare for Migration ==<br />
In some dir, create users.txt having all the user info<br />
e.g.)<br />
USERNAME = FirstName LastName <user@host><br />
<br />
== Migrate ==<br />
=== Without Tags ===<br />
$ git svn clone --stdlayout --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project-tmp <br />
(https://svn.repo/some-parent-folder/project-folder <- Not trunk)<br />
<br />
$ cd project-tmp <br />
<br />
$ git svn fetch <br />
<br />
* To view the other SVN branches<br />
$ git branch -r<br />
<br />
$ cd ..<br />
<br />
$ git clone project-tmp project <br />
$ rm -Rf project-tmp <br />
<br />
$ cd project <br />
$ git remote rm origin <br />
<br />
* Before this, add git repo for the project first.<br />
$ git remote add origin git@git-repo:project <br />
<br />
* To view all remote<br />
$ git remote -v <br />
<br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
<br />
<br />
* add user info to project/.git/config<br />
[user]<br />
email = user@host<br />
<br />
$ git push -u origin master <br />
<br />
* Check status<br />
$ git status <br />
# On branch master<br />
nothing to commit (working directory clean)<br />
<br />
<br />
=== With Tags ===<br />
If there are nested tag folders like <code>tags/released/1.x/1.1.0, tags/released/1.x/1.2.0, tags/released/1.x/1.3.0, tags/released/2.x/2.0.1 and tags/released/2.x/2.2.0</code>, add <code>--tags</code> (for branches, it's <code>--branches</code>)<br />
$ git svn clone --stdlayout --tags=tags/*/*/* --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project <br />
$ cd project<br />
$ git svn fetch <br />
<br />
<pre><br />
#### not necessary as it will be handled differently (by create tag and remove the svn tag) <br />
$ cp -Rf .git/refs/remotes/tags/* .git/refs/tags/ <br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ cp -Rf .git/refs/remotes/* .git/refs/heads/ <br />
$ rm -Rf .git/refs/remotes <br />
###################################################################################<br />
</pre><br />
<br />
$ git branch -r<br />
Then for each tag listed do:<br />
<br />
$ git tag tagname tags/tagname<br />
$ git branch -r -d tags/tagname<br />
<br />
$ git tag tag-name-for-git tags/tag-name-from-svn<br />
$ git branch -r -d tags/tag-name-from-svn<br />
<br />
e.g.)<br />
$ git tag 0.1.15 tags/released/0.x/some-app-0.1.15<br />
$ git branch -r -d tags/released/0.x/some-app-0.1.15<br />
<br />
* list tags<br />
$ git tag -l<br />
<br />
* remove old svn tags<br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ git remote add origin git@git-repo:project <br />
<br />
* add user info to project/.git/config <br />
$ git config -e<br />
<br />
[user]<br />
email = user@host<br />
<br />
and remove [svn-remote "svn"] and [svn]<br />
$ git push origin --all <br />
<br />
It doesn't seem like the tags are pushed?<br />
$ git push origin --tags</div>Kevinhttp://kevinlee.io/wiki/TomcatTomcat2015-03-21T12:01:45Z<p>Kevin: Redirected page to Apache Tomcat</p>
<hr />
<div>#REDIRECT [[Apache Tomcat]]</div>Kevinhttp://kevinlee.io/wiki/GitGit2015-03-07T03:16:41Z<p>Kevin: /* Branch */</p>
<hr />
<div>[[Category:Revision Control]]<br />
= Git =<br />
== Client-side Installation ==<br />
<br />
In the client,<br />
$ apt-get install git-core <br />
<br />
Or to install the latest version of Git on Ubuntu<br />
add-apt-repository ppa:git-core/ppa <br />
apt-get update <br />
apt-get install git-core <br />
<br />
== Configuration ==<br />
=== User Info ===<br />
$ git config --global user.name "GivenName Surname" <br />
$ git config --global user.email user@email.address <br />
<br />
=== Default Text Editor ===<br />
$ git config --global core.editor "sublime-text -w"<br />
<br />
=== External Diff Tool ===<br />
==== Meld As External Diff Tool ====<br />
* To use Meld as a diff tool, install Meld<br />
$ sudo apt-get install meld <br />
* Set it as the default diff tool<br />
$ git config --global diff.external meld <br />
<br />
* It doesn't work and gives the following error messages.<br />
<pre><br />
$ git diff <br />
Usage: <br />
meld Start with an empty window<br />
meld <file|dir> Start a version control comparison<br />
meld <file> <file> [<file>] Start a 2- or 3-way file comparison<br />
meld <dir> <dir> [<dir>] Start a 2- or 3-way directory comparison<br />
meld <file> <dir> Start a comparison between file and dir/file<br />
<br />
meld: error: too many arguments (wanted 0-4, got 7)<br />
</pre><br />
<br />
* Create ~/diff.py<br />
<pre><br />
#!/usr/bin/python<br />
<br />
import sys<br />
import os<br />
<br />
os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))<br />
</pre><br />
* Make it executable<br />
$ chmod a+x ~/diff.py <br />
* Set the script as the diff tool<br />
$ git config --global diff.external ~/diff.py <br />
* Now it works<br />
$ git diff <br />
$ git diff HEAD <br />
<br />
<br />
==== Meld as Diff Tool ====<br />
* This way is much easier.<br />
$ git config --global diff.tool meld <br />
<br />
$ git difftool<br />
<br />
==== Meld as Diff Tool ====<br />
$ git config --global diff.tool vimdiff <br />
<br />
<br />
=== Unset External Diff Tool ===<br />
$ git config --global --unset diff.external <br />
<br />
<br />
=== Commit Template ===<br />
Create a template file<br/><br />
e.g.) $HOME/git/.your-config/git-commit-comment-template.txt<br />
CLOSED - #TICKET_NUMBER: Summary<br />
TICKET_URI<br />
Details<br />
<br />
$ git config --global commit.template $HOME/git/.your-config/git-commit-comment-template.txt <br />
<br />
<br />
=== Etc. ===<br />
$ git config --global color.ui true<br />
<br />
== Server-side Installation ==<br />
In the server,<br />
$ apt-get install git <br />
<br />
= Gitolite =<br />
== Add User to Manage Gitolite ==<br />
# Add user to manage gitolite <br />
<br />
$ adduser \<br />
--system \<br />
--shell /bin/bash \<br />
--gecos 'git version control' \<br />
--group \<br />
--disabled-password \<br />
--home /home/git \<br />
git<br />
<br />
Adding system user `git' (UID 108) ...<br />
Adding new group `git' (GID 110) ...<br />
Adding new user `git' (UID 108) with group `git' ...<br />
Creating home directory `/home/git' ...<br />
<br />
== Create SSH key ==<br />
$ ssh-keygen -t rsa <br />
/home/username/.ssh/some_git_rsa<br />
[ENTER]<br />
[ENTER]<br />
<br />
== Transfer SSH Keys to Server ==<br />
$ ssh-copy-id '-p PORT_NUMBER -i /home/user/.ssh/some_git_rsa.pub git_user@host' <br />
If it doesn't work because the user is the only user who can access the server with his/her ssh key so accessing with git_user doesn't work.<br />
<br />
$ scp -P PORT ~/.ssh/some_git_rsa.pub user@host: <br />
<br />
Log in with the user account<br />
$ ssh -p PORT user@host<br />
$ mv some_git_rsa.pub git.pub <br />
<br />
== Setup ==<br />
$ su - git <br />
$ cd ~ <br />
$ echo "PATH=$HOME/bin:$PATH" >> ~/.bashrc <br />
<br />
* don't need these 2 lines<br />
$ mkdir .ssh <br />
$ cat /home/user/git.pub >> /home/git/.ssh/authorized_keys <br />
<br />
$ exit<br />
<br />
== Gitolite Installation ==<br />
$ sudo -i <br />
$ apt-get install gitolite<br />
[ENTER] -> :q to exit<br />
$ exit<br />
<br />
== Setup git User ==<br />
$ su - git<br />
$ gl-setup /home/user/git.pub <br />
<br />
$ vim /etc/ssh/sshd_config <br />
Add git to the allowed user<br />
<br />
AllowUsers user git<br />
<br />
Restart ssh<br />
$ service ssh restart <br />
<br />
== Test ==<br />
* Testing (from user local machine)<br />
$ ssh -p PORT_NUMBER git@host <br />
Having many keys?<br />
$ ssh -p PORT_NUMBER -i ~/.ssh/user@host1.pub git@host <br />
<br />
For debugging<br />
$ ssh -p PORT_NUMBER -vT git@host <br />
<br />
$ mkdir some-dir <br />
$ cd some-dir <br />
$ git clone git@host:gitolite-admin <br />
Or if you use a specific port,<br />
$ git clone ssh://git@host:PORT_NUMBER/gitolite-admin<br />
<br />
== Add Git User (through Gitolite) ==<br />
Adding User<br />
$ cd gitolite-admin/keydir <br />
$ mkdir user_name <br />
$ cp -p ~/.ssh/user@host1.pub .<br />
$ cp -p ~/.ssh/user@host2.pub .<br />
$ git add user@host1.pub <br />
$ git commit -a -m "users added: user@host1 and user@host2" <br />
$ git push<br />
<br />
If directly copied to <code>/home/git/.gitolite/keydir</code>, run<br />
$ gl-setup<br />
to apply the changes<br />
<br />
= Usage =<br />
== Initializing a Repository in an Existing Directory ==<br />
* To make a directory a git repository, go to the project directory and run the following command<br />
<pre><br />
$ git init <br />
</pre><br />
Then it will create a new directory named <code>.git</code> inside the project directory. The <code>.git</code> folder contains all necessary files for the git repository.<br />
<br />
== Host Info Config using SSH_Config ==<br />
Create <code>~/.ssh/config</code> file and edit.<br />
<br />
Host host.name<br />
Hostname host.name<br />
Port 1111<br />
<br />
Host another.host.name<br />
Hostname another.host.name<br />
Port 2222<br />
<br />
Host host1<br />
Hostname host.name<br />
Port 1111<br />
User git<br />
IdentityFile ~/.ssh/git_rsa<br />
<br />
Host user-host2<br />
Hostname host2.name<br />
Port 1234<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
Host user-host3<br />
Hostname host3.com<br />
Port 1122<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
'''IdentityFile is a private key'''<br />
<br />
To use this info,<br />
$ git clone git@host1:repo-name <br />
# it will use 'git' as a user and access the server 'host.name' using the port nubmer 1111.<br />
<br />
$ git clone git@host1:repo-name <br />
# it will use 'user' as a user and access the server 'host2.name' using the port nubmer 1234.<br />
<br />
== Push to Specific Host ==<br />
<br />
$ git push git@host1:repo-name master<br />
<br />
== Set up Project Specific User Info ==<br />
* Move to the project repository<br />
$ git config user.email user@email.address <br />
<br />
To see and edit all the config details<br />
$ git config -e<br />
<br />
== .gitignore ==<br />
To exclude files when committing, create .gitignore file and put in the project root (git project repo root).<br />
For maven project, the content should be<br />
target/<br />
<br />
To set up the global ignore file<br />
$ git config --global core.excludesfile $HOME/git/.your-config/.global-gitignore <br />
<br />
Other gitignore files,<br />
https://github.com/github/gitignore<br />
<br />
<br />
=== .gitignore for Eclipse and Maven (for Kevin) ===<br />
<pre><br />
target/<br />
<br />
*.pydevproject<br />
.project<br />
.classpath<br />
.settings/<br />
.metadata<br />
bin/<br />
tmp/<br />
*.tmp<br />
*.bak<br />
*.swp<br />
*~.nib<br />
local.properties<br />
.loadpath<br />
<br />
# External tool builders<br />
.externalToolBuilders/<br />
<br />
# Locally stored "Eclipse launch configurations"<br />
*.launch<br />
<br />
# CDT-specific<br />
.cproject<br />
<br />
# PDT-specific<br />
.buildpath<br />
</pre><br />
<br />
<br />
== Commit ==<br />
* Add a file<br />
$ git add fileName<br />
<br />
* Commit changes<br />
$ git commit -m "comment"<br />
<br />
* Add all the files and folders<br />
$ git add -A <br />
# It is equivalent to git add .; git add -u<br />
# git add . -> add all changed and new ones but not removed ones<br />
# git add -u -> changed and removed but not new ones.<br />
<br />
=== Amend Last Comment ===<br />
* Amend the last comment<br />
$ git commit --amend -m "New comment"<br />
<br />
== Push ==<br />
=== Add Remote ===<br />
* To view all remote<br />
<pre><br />
$ git remote -v <br />
</pre><br />
<br />
* Add a remote<br />
<pre><br />
$ git remote add origin git@git-repo:project <br />
</pre><br />
Then<br />
<pre><br />
$ git push -u origin master <br />
</pre><br />
Or<br />
<pre><br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
</pre><br />
=== Push to Remote ===<br />
* Push to the remote git repository<br />
$ git push<br />
<br />
* If it gives error messages like:<br />
No refs in common and none specified; doing nothing.<br />
Perhaps you should specify a branch such as 'master'.<br />
(It doesn't happen if you have already done <code>git push -u origin master </code>).<br />
* Do<br />
$ git push origin master<br />
<br />
== Revert ==<br />
=== Reset ===<br />
* Revert to last commit.<br />
$ git reset --hard HEAD <br />
Or to specific commit/tag/branch<br />
$ git reset --hard <tag/branch/commit id> <br />
<br />
* Check the id using git log<br />
$ git log <br />
<br />
* To push it<br />
$ git push <reponame> -f <br />
<br />
=== Check out ===<br />
* Or change the name of master branch then check out from the commit to be used and make it master<br />
<br />
# Rename the current master branch<br />
#: <pre>$ git branch -m another_name_for_master </pre> <br />
# Check out from another commit:<br />
#: <pre> $ git checkout a1b2c3a3blah </pre><br />
# Make it the new master branch:<br />
#: <pre> $ git checkout -b master </pre><br />
* If the old master is no longer needed and has to be removed, remove it by<br />
$ git branch -D another_name_for_master<br />
<br />
<br />
=== Restore Removed File ===<br />
'''This has to be tested first then should be rewritten'''.<br /><br />
Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.<br />
$ git rev-list -n 1 HEAD -- <file_path> <br />
<br />
Then checkout the version at the commit before.<br />
$ git checkout <deleting_commit>^ -- <file_path> <br />
<br />
Or in one command, if $file is the file.<br />
$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"<br />
* Reference<br />
[http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top]<br />
<br />
<br />
== Branch ==<br />
=== Create Branch ===<br />
* Create branch<br />
<pre><br />
$ git branch branch_name <br />
</pre><br />
* Change to the branch just created<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
OR<br />
* Create and change<br />
<pre><br />
git checkout -b branch_name <br />
</pre><br />
<br />
* Back to master<br />
<pre><br />
$ git checkout master <br />
</pre><br />
<br />
* To push,<br />
<pre><br />
$ git push origin branch_name <br />
</pre><br />
<br />
=== Change Branch ===<br />
* Change branch<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
<br />
* It does not work when the local git repo has multiple remote repositories. If it's the case,<br />
<pre><br />
$ git checkout -b branch remote/branch <br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ git checkout -b my-branch origin/my-branch <br />
</pre><br />
<br />
<br />
=== Pull Without Checkout ===<br />
* To pull a specific branch without checking out<br />
<pre><br />
$ git fetch <remote> <remoteBranch>:<localBranch> <br />
</pre><br />
e.g.)<br />
<pre><br />
git fetch origin master:master <br />
</pre><br />
<pre><br />
git fetch origin adding-new-feature:adding-new-feature <br />
</pre><br />
<br />
== Tag ==<br />
=== List Tags ===<br />
<pre><br />
$ git tag <br />
</pre><br />
<br />
=== View Tag ===<br />
* To view tag data with commit details.<br />
<pre><br />
$ git show tag_name <br />
</pre><br />
<br />
=== Create Tag ===<br />
* Create Tag<br />
<pre><br />
$ git tag -a tag_name -m 'tag message: new version blah blah' <br />
</pre><br />
* Tag later <br />
<pre><br />
$ git tag -a tag_name -m 'tag message' commit_checksum <br />
</pre><br />
e.g.)<br />
<pre><br />
$ git tag -a v1.5 -m 'new version with blah blah' b015e9d <br />
</pre><br />
* To see the list of checksum use the following command.<br />
<pre><br />
$ git log --pretty=oneline <br />
</pre><br />
<br />
=== Remove Tag ===<br />
<pre><br />
$ git tag -d tag_name <br />
</pre><br />
<br />
<br />
== Merge Multiple Git Repositories into One ==<br />
If the directory structures look like this<br />
<pre><br />
parent-dir (not git repo)<br />
│<br />
├─git-repo1 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
├─git-repo2 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
└─ git-repo3 ## each is connected to its own remote git repository<br />
├─src<br />
└.git<br />
</pre><br />
<br />
To make it like<br />
<pre><br />
parent-dir (now it is git repo)<br />
├─src ## contains all the files and sub-dirs from those three repositories (git-repo1, git-repo2 and git-repo3).<br />
└.git<br />
<br />
</pre><br />
and it still keeps all the histories from all those repositories, do following instructions.<br />
<br />
* Initialize the new repository directory.<br />
<pre><br />
$ cd parent-dir <br />
$ git init <br />
</pre><br />
<br />
* Pull the old ones and remove these one by one.<br />
<pre><br />
$ git pull git-repo1 <br />
<br />
## If there is any conflict, solve it first then<br />
## commit the changes<br />
$ git add file <br />
$ git commit -a <br />
## Now pull the one had conflict issue again.<br />
$ git pull git-repo1 <br />
<br />
$ rm -rf git-repo1 <br />
<br />
$ git pull git-repo2 <br />
$ rm -rf git-repo2 <br />
<br />
$ git pull git-repo3 <br />
$ rm -rf git-repo3 <br />
</pre><br />
<br />
* Done! Add the remote repo info if necessary.<br />
<br />
= Migration from SVN to Git =<br />
== Install git-svn ==<br />
$ apt-get install git-svn <br />
<br />
== Prepare for Migration ==<br />
In some dir, create users.txt having all the user info<br />
e.g.)<br />
USERNAME = FirstName LastName <user@host><br />
<br />
== Migrate ==<br />
=== Without Tags ===<br />
$ git svn clone --stdlayout --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project-tmp <br />
(https://svn.repo/some-parent-folder/project-folder <- Not trunk)<br />
<br />
$ cd project-tmp <br />
<br />
$ git svn fetch <br />
<br />
* To view the other SVN branches<br />
$ git branch -r<br />
<br />
$ cd ..<br />
<br />
$ git clone project-tmp project <br />
$ rm -Rf project-tmp <br />
<br />
$ cd project <br />
$ git remote rm origin <br />
<br />
* Before this, add git repo for the project first.<br />
$ git remote add origin git@git-repo:project <br />
<br />
* To view all remote<br />
$ git remote -v <br />
<br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
<br />
<br />
* add user info to project/.git/config<br />
[user]<br />
email = user@host<br />
<br />
$ git push -u origin master <br />
<br />
* Check status<br />
$ git status <br />
# On branch master<br />
nothing to commit (working directory clean)<br />
<br />
<br />
=== With Tags ===<br />
If there are nested tag folders like <code>tags/released/1.x/1.1.0, tags/released/1.x/1.2.0, tags/released/1.x/1.3.0, tags/released/2.x/2.0.1 and tags/released/2.x/2.2.0</code>, add <code>--tags</code> (for branches, it's <code>--branches</code>)<br />
$ git svn clone --stdlayout --tags=tags/*/*/* --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project <br />
$ cd project<br />
$ git svn fetch <br />
<br />
<pre><br />
#### not necessary as it will be handled differently (by create tag and remove the svn tag) <br />
$ cp -Rf .git/refs/remotes/tags/* .git/refs/tags/ <br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ cp -Rf .git/refs/remotes/* .git/refs/heads/ <br />
$ rm -Rf .git/refs/remotes <br />
###################################################################################<br />
</pre><br />
<br />
$ git branch -r<br />
Then for each tag listed do:<br />
<br />
$ git tag tagname tags/tagname<br />
$ git branch -r -d tags/tagname<br />
<br />
$ git tag tag-name-for-git tags/tag-name-from-svn<br />
$ git branch -r -d tags/tag-name-from-svn<br />
<br />
e.g.)<br />
$ git tag 0.1.15 tags/released/0.x/some-app-0.1.15<br />
$ git branch -r -d tags/released/0.x/some-app-0.1.15<br />
<br />
* list tags<br />
$ git tag -l<br />
<br />
* remove old svn tags<br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ git remote add origin git@git-repo:project <br />
<br />
* add user info to project/.git/config <br />
$ git config -e<br />
<br />
[user]<br />
email = user@host<br />
<br />
and remove [svn-remote "svn"] and [svn]<br />
$ git push origin --all <br />
<br />
It doesn't seem like the tags are pushed?<br />
$ git push origin --tags</div>Kevinhttp://kevinlee.io/wiki/GitGit2015-03-07T03:14:27Z<p>Kevin: /* Branch */</p>
<hr />
<div>[[Category:Revision Control]]<br />
= Git =<br />
== Client-side Installation ==<br />
<br />
In the client,<br />
$ apt-get install git-core <br />
<br />
Or to install the latest version of Git on Ubuntu<br />
add-apt-repository ppa:git-core/ppa <br />
apt-get update <br />
apt-get install git-core <br />
<br />
== Configuration ==<br />
=== User Info ===<br />
$ git config --global user.name "GivenName Surname" <br />
$ git config --global user.email user@email.address <br />
<br />
=== Default Text Editor ===<br />
$ git config --global core.editor "sublime-text -w"<br />
<br />
=== External Diff Tool ===<br />
==== Meld As External Diff Tool ====<br />
* To use Meld as a diff tool, install Meld<br />
$ sudo apt-get install meld <br />
* Set it as the default diff tool<br />
$ git config --global diff.external meld <br />
<br />
* It doesn't work and gives the following error messages.<br />
<pre><br />
$ git diff <br />
Usage: <br />
meld Start with an empty window<br />
meld <file|dir> Start a version control comparison<br />
meld <file> <file> [<file>] Start a 2- or 3-way file comparison<br />
meld <dir> <dir> [<dir>] Start a 2- or 3-way directory comparison<br />
meld <file> <dir> Start a comparison between file and dir/file<br />
<br />
meld: error: too many arguments (wanted 0-4, got 7)<br />
</pre><br />
<br />
* Create ~/diff.py<br />
<pre><br />
#!/usr/bin/python<br />
<br />
import sys<br />
import os<br />
<br />
os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))<br />
</pre><br />
* Make it executable<br />
$ chmod a+x ~/diff.py <br />
* Set the script as the diff tool<br />
$ git config --global diff.external ~/diff.py <br />
* Now it works<br />
$ git diff <br />
$ git diff HEAD <br />
<br />
<br />
==== Meld as Diff Tool ====<br />
* This way is much easier.<br />
$ git config --global diff.tool meld <br />
<br />
$ git difftool<br />
<br />
==== Meld as Diff Tool ====<br />
$ git config --global diff.tool vimdiff <br />
<br />
<br />
=== Unset External Diff Tool ===<br />
$ git config --global --unset diff.external <br />
<br />
<br />
=== Commit Template ===<br />
Create a template file<br/><br />
e.g.) $HOME/git/.your-config/git-commit-comment-template.txt<br />
CLOSED - #TICKET_NUMBER: Summary<br />
TICKET_URI<br />
Details<br />
<br />
$ git config --global commit.template $HOME/git/.your-config/git-commit-comment-template.txt <br />
<br />
<br />
=== Etc. ===<br />
$ git config --global color.ui true<br />
<br />
== Server-side Installation ==<br />
In the server,<br />
$ apt-get install git <br />
<br />
= Gitolite =<br />
== Add User to Manage Gitolite ==<br />
# Add user to manage gitolite <br />
<br />
$ adduser \<br />
--system \<br />
--shell /bin/bash \<br />
--gecos 'git version control' \<br />
--group \<br />
--disabled-password \<br />
--home /home/git \<br />
git<br />
<br />
Adding system user `git' (UID 108) ...<br />
Adding new group `git' (GID 110) ...<br />
Adding new user `git' (UID 108) with group `git' ...<br />
Creating home directory `/home/git' ...<br />
<br />
== Create SSH key ==<br />
$ ssh-keygen -t rsa <br />
/home/username/.ssh/some_git_rsa<br />
[ENTER]<br />
[ENTER]<br />
<br />
== Transfer SSH Keys to Server ==<br />
$ ssh-copy-id '-p PORT_NUMBER -i /home/user/.ssh/some_git_rsa.pub git_user@host' <br />
If it doesn't work because the user is the only user who can access the server with his/her ssh key so accessing with git_user doesn't work.<br />
<br />
$ scp -P PORT ~/.ssh/some_git_rsa.pub user@host: <br />
<br />
Log in with the user account<br />
$ ssh -p PORT user@host<br />
$ mv some_git_rsa.pub git.pub <br />
<br />
== Setup ==<br />
$ su - git <br />
$ cd ~ <br />
$ echo "PATH=$HOME/bin:$PATH" >> ~/.bashrc <br />
<br />
* don't need these 2 lines<br />
$ mkdir .ssh <br />
$ cat /home/user/git.pub >> /home/git/.ssh/authorized_keys <br />
<br />
$ exit<br />
<br />
== Gitolite Installation ==<br />
$ sudo -i <br />
$ apt-get install gitolite<br />
[ENTER] -> :q to exit<br />
$ exit<br />
<br />
== Setup git User ==<br />
$ su - git<br />
$ gl-setup /home/user/git.pub <br />
<br />
$ vim /etc/ssh/sshd_config <br />
Add git to the allowed user<br />
<br />
AllowUsers user git<br />
<br />
Restart ssh<br />
$ service ssh restart <br />
<br />
== Test ==<br />
* Testing (from user local machine)<br />
$ ssh -p PORT_NUMBER git@host <br />
Having many keys?<br />
$ ssh -p PORT_NUMBER -i ~/.ssh/user@host1.pub git@host <br />
<br />
For debugging<br />
$ ssh -p PORT_NUMBER -vT git@host <br />
<br />
$ mkdir some-dir <br />
$ cd some-dir <br />
$ git clone git@host:gitolite-admin <br />
Or if you use a specific port,<br />
$ git clone ssh://git@host:PORT_NUMBER/gitolite-admin<br />
<br />
== Add Git User (through Gitolite) ==<br />
Adding User<br />
$ cd gitolite-admin/keydir <br />
$ mkdir user_name <br />
$ cp -p ~/.ssh/user@host1.pub .<br />
$ cp -p ~/.ssh/user@host2.pub .<br />
$ git add user@host1.pub <br />
$ git commit -a -m "users added: user@host1 and user@host2" <br />
$ git push<br />
<br />
If directly copied to <code>/home/git/.gitolite/keydir</code>, run<br />
$ gl-setup<br />
to apply the changes<br />
<br />
= Usage =<br />
== Initializing a Repository in an Existing Directory ==<br />
* To make a directory a git repository, go to the project directory and run the following command<br />
<pre><br />
$ git init <br />
</pre><br />
Then it will create a new directory named <code>.git</code> inside the project directory. The <code>.git</code> folder contains all necessary files for the git repository.<br />
<br />
== Host Info Config using SSH_Config ==<br />
Create <code>~/.ssh/config</code> file and edit.<br />
<br />
Host host.name<br />
Hostname host.name<br />
Port 1111<br />
<br />
Host another.host.name<br />
Hostname another.host.name<br />
Port 2222<br />
<br />
Host host1<br />
Hostname host.name<br />
Port 1111<br />
User git<br />
IdentityFile ~/.ssh/git_rsa<br />
<br />
Host user-host2<br />
Hostname host2.name<br />
Port 1234<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
Host user-host3<br />
Hostname host3.com<br />
Port 1122<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
'''IdentityFile is a private key'''<br />
<br />
To use this info,<br />
$ git clone git@host1:repo-name <br />
# it will use 'git' as a user and access the server 'host.name' using the port nubmer 1111.<br />
<br />
$ git clone git@host1:repo-name <br />
# it will use 'user' as a user and access the server 'host2.name' using the port nubmer 1234.<br />
<br />
== Push to Specific Host ==<br />
<br />
$ git push git@host1:repo-name master<br />
<br />
== Set up Project Specific User Info ==<br />
* Move to the project repository<br />
$ git config user.email user@email.address <br />
<br />
To see and edit all the config details<br />
$ git config -e<br />
<br />
== .gitignore ==<br />
To exclude files when committing, create .gitignore file and put in the project root (git project repo root).<br />
For maven project, the content should be<br />
target/<br />
<br />
To set up the global ignore file<br />
$ git config --global core.excludesfile $HOME/git/.your-config/.global-gitignore <br />
<br />
Other gitignore files,<br />
https://github.com/github/gitignore<br />
<br />
<br />
=== .gitignore for Eclipse and Maven (for Kevin) ===<br />
<pre><br />
target/<br />
<br />
*.pydevproject<br />
.project<br />
.classpath<br />
.settings/<br />
.metadata<br />
bin/<br />
tmp/<br />
*.tmp<br />
*.bak<br />
*.swp<br />
*~.nib<br />
local.properties<br />
.loadpath<br />
<br />
# External tool builders<br />
.externalToolBuilders/<br />
<br />
# Locally stored "Eclipse launch configurations"<br />
*.launch<br />
<br />
# CDT-specific<br />
.cproject<br />
<br />
# PDT-specific<br />
.buildpath<br />
</pre><br />
<br />
<br />
== Commit ==<br />
* Add a file<br />
$ git add fileName<br />
<br />
* Commit changes<br />
$ git commit -m "comment"<br />
<br />
* Add all the files and folders<br />
$ git add -A <br />
# It is equivalent to git add .; git add -u<br />
# git add . -> add all changed and new ones but not removed ones<br />
# git add -u -> changed and removed but not new ones.<br />
<br />
=== Amend Last Comment ===<br />
* Amend the last comment<br />
$ git commit --amend -m "New comment"<br />
<br />
== Push ==<br />
=== Add Remote ===<br />
* To view all remote<br />
<pre><br />
$ git remote -v <br />
</pre><br />
<br />
* Add a remote<br />
<pre><br />
$ git remote add origin git@git-repo:project <br />
</pre><br />
Then<br />
<pre><br />
$ git push -u origin master <br />
</pre><br />
Or<br />
<pre><br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
</pre><br />
=== Push to Remote ===<br />
* Push to the remote git repository<br />
$ git push<br />
<br />
* If it gives error messages like:<br />
No refs in common and none specified; doing nothing.<br />
Perhaps you should specify a branch such as 'master'.<br />
(It doesn't happen if you have already done <code>git push -u origin master </code>).<br />
* Do<br />
$ git push origin master<br />
<br />
== Revert ==<br />
=== Reset ===<br />
* Revert to last commit.<br />
$ git reset --hard HEAD <br />
Or to specific commit/tag/branch<br />
$ git reset --hard <tag/branch/commit id> <br />
<br />
* Check the id using git log<br />
$ git log <br />
<br />
* To push it<br />
$ git push <reponame> -f <br />
<br />
=== Check out ===<br />
* Or change the name of master branch then check out from the commit to be used and make it master<br />
<br />
# Rename the current master branch<br />
#: <pre>$ git branch -m another_name_for_master </pre> <br />
# Check out from another commit:<br />
#: <pre> $ git checkout a1b2c3a3blah </pre><br />
# Make it the new master branch:<br />
#: <pre> $ git checkout -b master </pre><br />
* If the old master is no longer needed and has to be removed, remove it by<br />
$ git branch -D another_name_for_master<br />
<br />
<br />
=== Restore Removed File ===<br />
'''This has to be tested first then should be rewritten'''.<br /><br />
Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.<br />
$ git rev-list -n 1 HEAD -- <file_path> <br />
<br />
Then checkout the version at the commit before.<br />
$ git checkout <deleting_commit>^ -- <file_path> <br />
<br />
Or in one command, if $file is the file.<br />
$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"<br />
* Reference<br />
[http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top]<br />
<br />
<br />
== Branch ==<br />
* Create branch<br />
<pre><br />
$ git branch branch_name <br />
</pre><br />
* Change to the branch just created<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
OR<br />
* Create and change<br />
<pre><br />
git checkout -b branch_name <br />
</pre><br />
<br />
* Back to master<br />
<pre><br />
$ git checkout master <br />
</pre><br />
<br />
* To push,<br />
<pre><br />
$ git push origin branch_name <br />
</pre><br />
<br />
* Change branch<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
<br />
- It does not work when the local git repo has multiple remote repositories. If it's the case,<br />
<pre><br />
$ git checkout -b branch remote/branch <br />
</pre><br />
<br />
e.g.)<br />
<pre><br />
$ git checkout -b my-branch origin/my-branch <br />
</pre><br />
<br />
<br />
* To pull a specific branch without checking out<br />
<pre><br />
$ git fetch <remote> <remoteBranch>:<localBranch> <br />
</pre><br />
e.g.)<br />
<pre><br />
git fetch origin master:master <br />
</pre><br />
<pre><br />
git fetch origin adding-new-feature:adding-new-feature <br />
</pre><br />
<br />
== Tag ==<br />
=== List Tags ===<br />
<pre><br />
$ git tag <br />
</pre><br />
<br />
=== View Tag ===<br />
* To view tag data with commit details.<br />
<pre><br />
$ git show tag_name <br />
</pre><br />
<br />
=== Create Tag ===<br />
* Create Tag<br />
<pre><br />
$ git tag -a tag_name -m 'tag message: new version blah blah' <br />
</pre><br />
* Tag later <br />
<pre><br />
$ git tag -a tag_name -m 'tag message' commit_checksum <br />
</pre><br />
e.g.)<br />
<pre><br />
$ git tag -a v1.5 -m 'new version with blah blah' b015e9d <br />
</pre><br />
* To see the list of checksum use the following command.<br />
<pre><br />
$ git log --pretty=oneline <br />
</pre><br />
<br />
=== Remove Tag ===<br />
<pre><br />
$ git tag -d tag_name <br />
</pre><br />
<br />
<br />
== Merge Multiple Git Repositories into One ==<br />
If the directory structures look like this<br />
<pre><br />
parent-dir (not git repo)<br />
│<br />
├─git-repo1 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
├─git-repo2 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
└─ git-repo3 ## each is connected to its own remote git repository<br />
├─src<br />
└.git<br />
</pre><br />
<br />
To make it like<br />
<pre><br />
parent-dir (now it is git repo)<br />
├─src ## contains all the files and sub-dirs from those three repositories (git-repo1, git-repo2 and git-repo3).<br />
└.git<br />
<br />
</pre><br />
and it still keeps all the histories from all those repositories, do following instructions.<br />
<br />
* Initialize the new repository directory.<br />
<pre><br />
$ cd parent-dir <br />
$ git init <br />
</pre><br />
<br />
* Pull the old ones and remove these one by one.<br />
<pre><br />
$ git pull git-repo1 <br />
<br />
## If there is any conflict, solve it first then<br />
## commit the changes<br />
$ git add file <br />
$ git commit -a <br />
## Now pull the one had conflict issue again.<br />
$ git pull git-repo1 <br />
<br />
$ rm -rf git-repo1 <br />
<br />
$ git pull git-repo2 <br />
$ rm -rf git-repo2 <br />
<br />
$ git pull git-repo3 <br />
$ rm -rf git-repo3 <br />
</pre><br />
<br />
* Done! Add the remote repo info if necessary.<br />
<br />
= Migration from SVN to Git =<br />
== Install git-svn ==<br />
$ apt-get install git-svn <br />
<br />
== Prepare for Migration ==<br />
In some dir, create users.txt having all the user info<br />
e.g.)<br />
USERNAME = FirstName LastName <user@host><br />
<br />
== Migrate ==<br />
=== Without Tags ===<br />
$ git svn clone --stdlayout --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project-tmp <br />
(https://svn.repo/some-parent-folder/project-folder <- Not trunk)<br />
<br />
$ cd project-tmp <br />
<br />
$ git svn fetch <br />
<br />
* To view the other SVN branches<br />
$ git branch -r<br />
<br />
$ cd ..<br />
<br />
$ git clone project-tmp project <br />
$ rm -Rf project-tmp <br />
<br />
$ cd project <br />
$ git remote rm origin <br />
<br />
* Before this, add git repo for the project first.<br />
$ git remote add origin git@git-repo:project <br />
<br />
* To view all remote<br />
$ git remote -v <br />
<br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
<br />
<br />
* add user info to project/.git/config<br />
[user]<br />
email = user@host<br />
<br />
$ git push -u origin master <br />
<br />
* Check status<br />
$ git status <br />
# On branch master<br />
nothing to commit (working directory clean)<br />
<br />
<br />
=== With Tags ===<br />
If there are nested tag folders like <code>tags/released/1.x/1.1.0, tags/released/1.x/1.2.0, tags/released/1.x/1.3.0, tags/released/2.x/2.0.1 and tags/released/2.x/2.2.0</code>, add <code>--tags</code> (for branches, it's <code>--branches</code>)<br />
$ git svn clone --stdlayout --tags=tags/*/*/* --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project <br />
$ cd project<br />
$ git svn fetch <br />
<br />
<pre><br />
#### not necessary as it will be handled differently (by create tag and remove the svn tag) <br />
$ cp -Rf .git/refs/remotes/tags/* .git/refs/tags/ <br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ cp -Rf .git/refs/remotes/* .git/refs/heads/ <br />
$ rm -Rf .git/refs/remotes <br />
###################################################################################<br />
</pre><br />
<br />
$ git branch -r<br />
Then for each tag listed do:<br />
<br />
$ git tag tagname tags/tagname<br />
$ git branch -r -d tags/tagname<br />
<br />
$ git tag tag-name-for-git tags/tag-name-from-svn<br />
$ git branch -r -d tags/tag-name-from-svn<br />
<br />
e.g.)<br />
$ git tag 0.1.15 tags/released/0.x/some-app-0.1.15<br />
$ git branch -r -d tags/released/0.x/some-app-0.1.15<br />
<br />
* list tags<br />
$ git tag -l<br />
<br />
* remove old svn tags<br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ git remote add origin git@git-repo:project <br />
<br />
* add user info to project/.git/config <br />
$ git config -e<br />
<br />
[user]<br />
email = user@host<br />
<br />
and remove [svn-remote "svn"] and [svn]<br />
$ git push origin --all <br />
<br />
It doesn't seem like the tags are pushed?<br />
$ git push origin --tags</div>Kevinhttp://kevinlee.io/wiki/GitGit2015-01-06T12:30:37Z<p>Kevin: /* Branch */</p>
<hr />
<div>[[Category:Revision Control]]<br />
= Git =<br />
== Client-side Installation ==<br />
<br />
In the client,<br />
$ apt-get install git-core <br />
<br />
Or to install the latest version of Git on Ubuntu<br />
add-apt-repository ppa:git-core/ppa <br />
apt-get update <br />
apt-get install git-core <br />
<br />
== Configuration ==<br />
=== User Info ===<br />
$ git config --global user.name "GivenName Surname" <br />
$ git config --global user.email user@email.address <br />
<br />
=== Default Text Editor ===<br />
$ git config --global core.editor "sublime-text -w"<br />
<br />
=== External Diff Tool ===<br />
==== Meld As External Diff Tool ====<br />
* To use Meld as a diff tool, install Meld<br />
$ sudo apt-get install meld <br />
* Set it as the default diff tool<br />
$ git config --global diff.external meld <br />
<br />
* It doesn't work and gives the following error messages.<br />
<pre><br />
$ git diff <br />
Usage: <br />
meld Start with an empty window<br />
meld <file|dir> Start a version control comparison<br />
meld <file> <file> [<file>] Start a 2- or 3-way file comparison<br />
meld <dir> <dir> [<dir>] Start a 2- or 3-way directory comparison<br />
meld <file> <dir> Start a comparison between file and dir/file<br />
<br />
meld: error: too many arguments (wanted 0-4, got 7)<br />
</pre><br />
<br />
* Create ~/diff.py<br />
<pre><br />
#!/usr/bin/python<br />
<br />
import sys<br />
import os<br />
<br />
os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))<br />
</pre><br />
* Make it executable<br />
$ chmod a+x ~/diff.py <br />
* Set the script as the diff tool<br />
$ git config --global diff.external ~/diff.py <br />
* Now it works<br />
$ git diff <br />
$ git diff HEAD <br />
<br />
<br />
==== Meld as Diff Tool ====<br />
* This way is much easier.<br />
$ git config --global diff.tool meld <br />
<br />
$ git difftool<br />
<br />
==== Meld as Diff Tool ====<br />
$ git config --global diff.tool vimdiff <br />
<br />
<br />
=== Unset External Diff Tool ===<br />
$ git config --global --unset diff.external <br />
<br />
<br />
=== Commit Template ===<br />
Create a template file<br/><br />
e.g.) $HOME/git/.your-config/git-commit-comment-template.txt<br />
CLOSED - #TICKET_NUMBER: Summary<br />
TICKET_URI<br />
Details<br />
<br />
$ git config --global commit.template $HOME/git/.your-config/git-commit-comment-template.txt <br />
<br />
<br />
=== Etc. ===<br />
$ git config --global color.ui true<br />
<br />
== Server-side Installation ==<br />
In the server,<br />
$ apt-get install git <br />
<br />
= Gitolite =<br />
== Add User to Manage Gitolite ==<br />
# Add user to manage gitolite <br />
<br />
$ adduser \<br />
--system \<br />
--shell /bin/bash \<br />
--gecos 'git version control' \<br />
--group \<br />
--disabled-password \<br />
--home /home/git \<br />
git<br />
<br />
Adding system user `git' (UID 108) ...<br />
Adding new group `git' (GID 110) ...<br />
Adding new user `git' (UID 108) with group `git' ...<br />
Creating home directory `/home/git' ...<br />
<br />
== Create SSH key ==<br />
$ ssh-keygen -t rsa <br />
/home/username/.ssh/some_git_rsa<br />
[ENTER]<br />
[ENTER]<br />
<br />
== Transfer SSH Keys to Server ==<br />
$ ssh-copy-id '-p PORT_NUMBER -i /home/user/.ssh/some_git_rsa.pub git_user@host' <br />
If it doesn't work because the user is the only user who can access the server with his/her ssh key so accessing with git_user doesn't work.<br />
<br />
$ scp -P PORT ~/.ssh/some_git_rsa.pub user@host: <br />
<br />
Log in with the user account<br />
$ ssh -p PORT user@host<br />
$ mv some_git_rsa.pub git.pub <br />
<br />
== Setup ==<br />
$ su - git <br />
$ cd ~ <br />
$ echo "PATH=$HOME/bin:$PATH" >> ~/.bashrc <br />
<br />
* don't need these 2 lines<br />
$ mkdir .ssh <br />
$ cat /home/user/git.pub >> /home/git/.ssh/authorized_keys <br />
<br />
$ exit<br />
<br />
== Gitolite Installation ==<br />
$ sudo -i <br />
$ apt-get install gitolite<br />
[ENTER] -> :q to exit<br />
$ exit<br />
<br />
== Setup git User ==<br />
$ su - git<br />
$ gl-setup /home/user/git.pub <br />
<br />
$ vim /etc/ssh/sshd_config <br />
Add git to the allowed user<br />
<br />
AllowUsers user git<br />
<br />
Restart ssh<br />
$ service ssh restart <br />
<br />
== Test ==<br />
* Testing (from user local machine)<br />
$ ssh -p PORT_NUMBER git@host <br />
Having many keys?<br />
$ ssh -p PORT_NUMBER -i ~/.ssh/user@host1.pub git@host <br />
<br />
For debugging<br />
$ ssh -p PORT_NUMBER -vT git@host <br />
<br />
$ mkdir some-dir <br />
$ cd some-dir <br />
$ git clone git@host:gitolite-admin <br />
Or if you use a specific port,<br />
$ git clone ssh://git@host:PORT_NUMBER/gitolite-admin<br />
<br />
== Add Git User (through Gitolite) ==<br />
Adding User<br />
$ cd gitolite-admin/keydir <br />
$ mkdir user_name <br />
$ cp -p ~/.ssh/user@host1.pub .<br />
$ cp -p ~/.ssh/user@host2.pub .<br />
$ git add user@host1.pub <br />
$ git commit -a -m "users added: user@host1 and user@host2" <br />
$ git push<br />
<br />
If directly copied to <code>/home/git/.gitolite/keydir</code>, run<br />
$ gl-setup<br />
to apply the changes<br />
<br />
= Usage =<br />
== Initializing a Repository in an Existing Directory ==<br />
* To make a directory a git repository, go to the project directory and run the following command<br />
<pre><br />
$ git init <br />
</pre><br />
Then it will create a new directory named <code>.git</code> inside the project directory. The <code>.git</code> folder contains all necessary files for the git repository.<br />
<br />
== Host Info Config using SSH_Config ==<br />
Create <code>~/.ssh/config</code> file and edit.<br />
<br />
Host host.name<br />
Hostname host.name<br />
Port 1111<br />
<br />
Host another.host.name<br />
Hostname another.host.name<br />
Port 2222<br />
<br />
Host host1<br />
Hostname host.name<br />
Port 1111<br />
User git<br />
IdentityFile ~/.ssh/git_rsa<br />
<br />
Host user-host2<br />
Hostname host2.name<br />
Port 1234<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
Host user-host3<br />
Hostname host3.com<br />
Port 1122<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
'''IdentityFile is a private key'''<br />
<br />
To use this info,<br />
$ git clone git@host1:repo-name <br />
# it will use 'git' as a user and access the server 'host.name' using the port nubmer 1111.<br />
<br />
$ git clone git@host1:repo-name <br />
# it will use 'user' as a user and access the server 'host2.name' using the port nubmer 1234.<br />
<br />
== Push to Specific Host ==<br />
<br />
$ git push git@host1:repo-name master<br />
<br />
== Set up Project Specific User Info ==<br />
* Move to the project repository<br />
$ git config user.email user@email.address <br />
<br />
To see and edit all the config details<br />
$ git config -e<br />
<br />
== .gitignore ==<br />
To exclude files when committing, create .gitignore file and put in the project root (git project repo root).<br />
For maven project, the content should be<br />
target/<br />
<br />
To set up the global ignore file<br />
$ git config --global core.excludesfile $HOME/git/.your-config/.global-gitignore <br />
<br />
Other gitignore files,<br />
https://github.com/github/gitignore<br />
<br />
<br />
=== .gitignore for Eclipse and Maven (for Kevin) ===<br />
<pre><br />
target/<br />
<br />
*.pydevproject<br />
.project<br />
.classpath<br />
.settings/<br />
.metadata<br />
bin/<br />
tmp/<br />
*.tmp<br />
*.bak<br />
*.swp<br />
*~.nib<br />
local.properties<br />
.loadpath<br />
<br />
# External tool builders<br />
.externalToolBuilders/<br />
<br />
# Locally stored "Eclipse launch configurations"<br />
*.launch<br />
<br />
# CDT-specific<br />
.cproject<br />
<br />
# PDT-specific<br />
.buildpath<br />
</pre><br />
<br />
<br />
== Commit ==<br />
* Add a file<br />
$ git add fileName<br />
<br />
* Commit changes<br />
$ git commit -m "comment"<br />
<br />
* Add all the files and folders<br />
$ git add -A <br />
# It is equivalent to git add .; git add -u<br />
# git add . -> add all changed and new ones but not removed ones<br />
# git add -u -> changed and removed but not new ones.<br />
<br />
=== Amend Last Comment ===<br />
* Amend the last comment<br />
$ git commit --amend -m "New comment"<br />
<br />
== Push ==<br />
=== Add Remote ===<br />
* To view all remote<br />
<pre><br />
$ git remote -v <br />
</pre><br />
<br />
* Add a remote<br />
<pre><br />
$ git remote add origin git@git-repo:project <br />
</pre><br />
Then<br />
<pre><br />
$ git push -u origin master <br />
</pre><br />
Or<br />
<pre><br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
</pre><br />
=== Push to Remote ===<br />
* Push to the remote git repository<br />
$ git push<br />
<br />
* If it gives error messages like:<br />
No refs in common and none specified; doing nothing.<br />
Perhaps you should specify a branch such as 'master'.<br />
(It doesn't happen if you have already done <code>git push -u origin master </code>).<br />
* Do<br />
$ git push origin master<br />
<br />
== Revert ==<br />
=== Reset ===<br />
* Revert to last commit.<br />
$ git reset --hard HEAD <br />
Or to specific commit/tag/branch<br />
$ git reset --hard <tag/branch/commit id> <br />
<br />
* Check the id using git log<br />
$ git log <br />
<br />
* To push it<br />
$ git push <reponame> -f <br />
<br />
=== Check out ===<br />
* Or change the name of master branch then check out from the commit to be used and make it master<br />
<br />
# Rename the current master branch<br />
#: <pre>$ git branch -m another_name_for_master </pre> <br />
# Check out from another commit:<br />
#: <pre> $ git checkout a1b2c3a3blah </pre><br />
# Make it the new master branch:<br />
#: <pre> $ git checkout -b master </pre><br />
* If the old master is no longer needed and has to be removed, remove it by<br />
$ git branch -D another_name_for_master<br />
<br />
<br />
=== Restore Removed File ===<br />
'''This has to be tested first then should be rewritten'''.<br /><br />
Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.<br />
$ git rev-list -n 1 HEAD -- <file_path> <br />
<br />
Then checkout the version at the commit before.<br />
$ git checkout <deleting_commit>^ -- <file_path> <br />
<br />
Or in one command, if $file is the file.<br />
$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"<br />
* Reference<br />
[http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top]<br />
<br />
<br />
== Branch ==<br />
* Create branch<br />
<pre><br />
$ git branch branch_name <br />
</pre><br />
* Change to the branch just created<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
OR<br />
* Create and change<br />
<pre><br />
git checkout -b branch_name <br />
</pre><br />
<br />
* Back to master<br />
<pre><br />
$ git checkout master <br />
</pre><br />
<br />
* To push,<br />
<pre><br />
$ git push origin branch_name <br />
</pre><br />
<br />
* To pull a specific branch without checking out<br />
<pre><br />
$ git fetch <remote> <remoteBranch>:<localBranch> <br />
</pre><br />
e.g.)<br />
<pre><br />
git fetch origin master:master <br />
</pre><br />
<pre><br />
git fetch origin adding-new-feature:adding-new-feature <br />
</pre><br />
<br />
== Tag ==<br />
=== List Tags ===<br />
<pre><br />
$ git tag <br />
</pre><br />
<br />
=== View Tag ===<br />
* To view tag data with commit details.<br />
<pre><br />
$ git show tag_name <br />
</pre><br />
<br />
=== Create Tag ===<br />
* Create Tag<br />
<pre><br />
$ git tag -a tag_name -m 'tag message: new version blah blah' <br />
</pre><br />
* Tag later <br />
<pre><br />
$ git tag -a tag_name -m 'tag message' commit_checksum <br />
</pre><br />
e.g.)<br />
<pre><br />
$ git tag -a v1.5 -m 'new version with blah blah' b015e9d <br />
</pre><br />
* To see the list of checksum use the following command.<br />
<pre><br />
$ git log --pretty=oneline <br />
</pre><br />
<br />
=== Remove Tag ===<br />
<pre><br />
$ git tag -d tag_name <br />
</pre><br />
<br />
<br />
== Merge Multiple Git Repositories into One ==<br />
If the directory structures look like this<br />
<pre><br />
parent-dir (not git repo)<br />
│<br />
├─git-repo1 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
├─git-repo2 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
└─ git-repo3 ## each is connected to its own remote git repository<br />
├─src<br />
└.git<br />
</pre><br />
<br />
To make it like<br />
<pre><br />
parent-dir (now it is git repo)<br />
├─src ## contains all the files and sub-dirs from those three repositories (git-repo1, git-repo2 and git-repo3).<br />
└.git<br />
<br />
</pre><br />
and it still keeps all the histories from all those repositories, do following instructions.<br />
<br />
* Initialize the new repository directory.<br />
<pre><br />
$ cd parent-dir <br />
$ git init <br />
</pre><br />
<br />
* Pull the old ones and remove these one by one.<br />
<pre><br />
$ git pull git-repo1 <br />
<br />
## If there is any conflict, solve it first then<br />
## commit the changes<br />
$ git add file <br />
$ git commit -a <br />
## Now pull the one had conflict issue again.<br />
$ git pull git-repo1 <br />
<br />
$ rm -rf git-repo1 <br />
<br />
$ git pull git-repo2 <br />
$ rm -rf git-repo2 <br />
<br />
$ git pull git-repo3 <br />
$ rm -rf git-repo3 <br />
</pre><br />
<br />
* Done! Add the remote repo info if necessary.<br />
<br />
= Migration from SVN to Git =<br />
== Install git-svn ==<br />
$ apt-get install git-svn <br />
<br />
== Prepare for Migration ==<br />
In some dir, create users.txt having all the user info<br />
e.g.)<br />
USERNAME = FirstName LastName <user@host><br />
<br />
== Migrate ==<br />
=== Without Tags ===<br />
$ git svn clone --stdlayout --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project-tmp <br />
(https://svn.repo/some-parent-folder/project-folder <- Not trunk)<br />
<br />
$ cd project-tmp <br />
<br />
$ git svn fetch <br />
<br />
* To view the other SVN branches<br />
$ git branch -r<br />
<br />
$ cd ..<br />
<br />
$ git clone project-tmp project <br />
$ rm -Rf project-tmp <br />
<br />
$ cd project <br />
$ git remote rm origin <br />
<br />
* Before this, add git repo for the project first.<br />
$ git remote add origin git@git-repo:project <br />
<br />
* To view all remote<br />
$ git remote -v <br />
<br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
<br />
<br />
* add user info to project/.git/config<br />
[user]<br />
email = user@host<br />
<br />
$ git push -u origin master <br />
<br />
* Check status<br />
$ git status <br />
# On branch master<br />
nothing to commit (working directory clean)<br />
<br />
<br />
=== With Tags ===<br />
If there are nested tag folders like <code>tags/released/1.x/1.1.0, tags/released/1.x/1.2.0, tags/released/1.x/1.3.0, tags/released/2.x/2.0.1 and tags/released/2.x/2.2.0</code>, add <code>--tags</code> (for branches, it's <code>--branches</code>)<br />
$ git svn clone --stdlayout --tags=tags/*/*/* --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project <br />
$ cd project<br />
$ git svn fetch <br />
<br />
<pre><br />
#### not necessary as it will be handled differently (by create tag and remove the svn tag) <br />
$ cp -Rf .git/refs/remotes/tags/* .git/refs/tags/ <br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ cp -Rf .git/refs/remotes/* .git/refs/heads/ <br />
$ rm -Rf .git/refs/remotes <br />
###################################################################################<br />
</pre><br />
<br />
$ git branch -r<br />
Then for each tag listed do:<br />
<br />
$ git tag tagname tags/tagname<br />
$ git branch -r -d tags/tagname<br />
<br />
$ git tag tag-name-for-git tags/tag-name-from-svn<br />
$ git branch -r -d tags/tag-name-from-svn<br />
<br />
e.g.)<br />
$ git tag 0.1.15 tags/released/0.x/some-app-0.1.15<br />
$ git branch -r -d tags/released/0.x/some-app-0.1.15<br />
<br />
* list tags<br />
$ git tag -l<br />
<br />
* remove old svn tags<br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ git remote add origin git@git-repo:project <br />
<br />
* add user info to project/.git/config <br />
$ git config -e<br />
<br />
[user]<br />
email = user@host<br />
<br />
and remove [svn-remote "svn"] and [svn]<br />
$ git push origin --all <br />
<br />
It doesn't seem like the tags are pushed?<br />
$ git push origin --tags</div>Kevinhttp://kevinlee.io/wiki/GitGit2014-12-27T05:56:21Z<p>Kevin: </p>
<hr />
<div>[[Category:Revision Control]]<br />
= Git =<br />
== Client-side Installation ==<br />
<br />
In the client,<br />
$ apt-get install git-core <br />
<br />
Or to install the latest version of Git on Ubuntu<br />
add-apt-repository ppa:git-core/ppa <br />
apt-get update <br />
apt-get install git-core <br />
<br />
== Configuration ==<br />
=== User Info ===<br />
$ git config --global user.name "GivenName Surname" <br />
$ git config --global user.email user@email.address <br />
<br />
=== Default Text Editor ===<br />
$ git config --global core.editor "sublime-text -w"<br />
<br />
=== External Diff Tool ===<br />
==== Meld As External Diff Tool ====<br />
* To use Meld as a diff tool, install Meld<br />
$ sudo apt-get install meld <br />
* Set it as the default diff tool<br />
$ git config --global diff.external meld <br />
<br />
* It doesn't work and gives the following error messages.<br />
<pre><br />
$ git diff <br />
Usage: <br />
meld Start with an empty window<br />
meld <file|dir> Start a version control comparison<br />
meld <file> <file> [<file>] Start a 2- or 3-way file comparison<br />
meld <dir> <dir> [<dir>] Start a 2- or 3-way directory comparison<br />
meld <file> <dir> Start a comparison between file and dir/file<br />
<br />
meld: error: too many arguments (wanted 0-4, got 7)<br />
</pre><br />
<br />
* Create ~/diff.py<br />
<pre><br />
#!/usr/bin/python<br />
<br />
import sys<br />
import os<br />
<br />
os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))<br />
</pre><br />
* Make it executable<br />
$ chmod a+x ~/diff.py <br />
* Set the script as the diff tool<br />
$ git config --global diff.external ~/diff.py <br />
* Now it works<br />
$ git diff <br />
$ git diff HEAD <br />
<br />
<br />
==== Meld as Diff Tool ====<br />
* This way is much easier.<br />
$ git config --global diff.tool meld <br />
<br />
$ git difftool<br />
<br />
==== Meld as Diff Tool ====<br />
$ git config --global diff.tool vimdiff <br />
<br />
<br />
=== Unset External Diff Tool ===<br />
$ git config --global --unset diff.external <br />
<br />
<br />
=== Commit Template ===<br />
Create a template file<br/><br />
e.g.) $HOME/git/.your-config/git-commit-comment-template.txt<br />
CLOSED - #TICKET_NUMBER: Summary<br />
TICKET_URI<br />
Details<br />
<br />
$ git config --global commit.template $HOME/git/.your-config/git-commit-comment-template.txt <br />
<br />
<br />
=== Etc. ===<br />
$ git config --global color.ui true<br />
<br />
== Server-side Installation ==<br />
In the server,<br />
$ apt-get install git <br />
<br />
= Gitolite =<br />
== Add User to Manage Gitolite ==<br />
# Add user to manage gitolite <br />
<br />
$ adduser \<br />
--system \<br />
--shell /bin/bash \<br />
--gecos 'git version control' \<br />
--group \<br />
--disabled-password \<br />
--home /home/git \<br />
git<br />
<br />
Adding system user `git' (UID 108) ...<br />
Adding new group `git' (GID 110) ...<br />
Adding new user `git' (UID 108) with group `git' ...<br />
Creating home directory `/home/git' ...<br />
<br />
== Create SSH key ==<br />
$ ssh-keygen -t rsa <br />
/home/username/.ssh/some_git_rsa<br />
[ENTER]<br />
[ENTER]<br />
<br />
== Transfer SSH Keys to Server ==<br />
$ ssh-copy-id '-p PORT_NUMBER -i /home/user/.ssh/some_git_rsa.pub git_user@host' <br />
If it doesn't work because the user is the only user who can access the server with his/her ssh key so accessing with git_user doesn't work.<br />
<br />
$ scp -P PORT ~/.ssh/some_git_rsa.pub user@host: <br />
<br />
Log in with the user account<br />
$ ssh -p PORT user@host<br />
$ mv some_git_rsa.pub git.pub <br />
<br />
== Setup ==<br />
$ su - git <br />
$ cd ~ <br />
$ echo "PATH=$HOME/bin:$PATH" >> ~/.bashrc <br />
<br />
* don't need these 2 lines<br />
$ mkdir .ssh <br />
$ cat /home/user/git.pub >> /home/git/.ssh/authorized_keys <br />
<br />
$ exit<br />
<br />
== Gitolite Installation ==<br />
$ sudo -i <br />
$ apt-get install gitolite<br />
[ENTER] -> :q to exit<br />
$ exit<br />
<br />
== Setup git User ==<br />
$ su - git<br />
$ gl-setup /home/user/git.pub <br />
<br />
$ vim /etc/ssh/sshd_config <br />
Add git to the allowed user<br />
<br />
AllowUsers user git<br />
<br />
Restart ssh<br />
$ service ssh restart <br />
<br />
== Test ==<br />
* Testing (from user local machine)<br />
$ ssh -p PORT_NUMBER git@host <br />
Having many keys?<br />
$ ssh -p PORT_NUMBER -i ~/.ssh/user@host1.pub git@host <br />
<br />
For debugging<br />
$ ssh -p PORT_NUMBER -vT git@host <br />
<br />
$ mkdir some-dir <br />
$ cd some-dir <br />
$ git clone git@host:gitolite-admin <br />
Or if you use a specific port,<br />
$ git clone ssh://git@host:PORT_NUMBER/gitolite-admin<br />
<br />
== Add Git User (through Gitolite) ==<br />
Adding User<br />
$ cd gitolite-admin/keydir <br />
$ mkdir user_name <br />
$ cp -p ~/.ssh/user@host1.pub .<br />
$ cp -p ~/.ssh/user@host2.pub .<br />
$ git add user@host1.pub <br />
$ git commit -a -m "users added: user@host1 and user@host2" <br />
$ git push<br />
<br />
If directly copied to <code>/home/git/.gitolite/keydir</code>, run<br />
$ gl-setup<br />
to apply the changes<br />
<br />
= Usage =<br />
== Initializing a Repository in an Existing Directory ==<br />
* To make a directory a git repository, go to the project directory and run the following command<br />
<pre><br />
$ git init <br />
</pre><br />
Then it will create a new directory named <code>.git</code> inside the project directory. The <code>.git</code> folder contains all necessary files for the git repository.<br />
<br />
== Host Info Config using SSH_Config ==<br />
Create <code>~/.ssh/config</code> file and edit.<br />
<br />
Host host.name<br />
Hostname host.name<br />
Port 1111<br />
<br />
Host another.host.name<br />
Hostname another.host.name<br />
Port 2222<br />
<br />
Host host1<br />
Hostname host.name<br />
Port 1111<br />
User git<br />
IdentityFile ~/.ssh/git_rsa<br />
<br />
Host user-host2<br />
Hostname host2.name<br />
Port 1234<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
Host user-host3<br />
Hostname host3.com<br />
Port 1122<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
'''IdentityFile is a private key'''<br />
<br />
To use this info,<br />
$ git clone git@host1:repo-name <br />
# it will use 'git' as a user and access the server 'host.name' using the port nubmer 1111.<br />
<br />
$ git clone git@host1:repo-name <br />
# it will use 'user' as a user and access the server 'host2.name' using the port nubmer 1234.<br />
<br />
== Push to Specific Host ==<br />
<br />
$ git push git@host1:repo-name master<br />
<br />
== Set up Project Specific User Info ==<br />
* Move to the project repository<br />
$ git config user.email user@email.address <br />
<br />
To see and edit all the config details<br />
$ git config -e<br />
<br />
== .gitignore ==<br />
To exclude files when committing, create .gitignore file and put in the project root (git project repo root).<br />
For maven project, the content should be<br />
target/<br />
<br />
To set up the global ignore file<br />
$ git config --global core.excludesfile $HOME/git/.your-config/.global-gitignore <br />
<br />
Other gitignore files,<br />
https://github.com/github/gitignore<br />
<br />
<br />
=== .gitignore for Eclipse and Maven (for Kevin) ===<br />
<pre><br />
target/<br />
<br />
*.pydevproject<br />
.project<br />
.classpath<br />
.settings/<br />
.metadata<br />
bin/<br />
tmp/<br />
*.tmp<br />
*.bak<br />
*.swp<br />
*~.nib<br />
local.properties<br />
.loadpath<br />
<br />
# External tool builders<br />
.externalToolBuilders/<br />
<br />
# Locally stored "Eclipse launch configurations"<br />
*.launch<br />
<br />
# CDT-specific<br />
.cproject<br />
<br />
# PDT-specific<br />
.buildpath<br />
</pre><br />
<br />
<br />
== Commit ==<br />
* Add a file<br />
$ git add fileName<br />
<br />
* Commit changes<br />
$ git commit -m "comment"<br />
<br />
* Add all the files and folders<br />
$ git add -A <br />
# It is equivalent to git add .; git add -u<br />
# git add . -> add all changed and new ones but not removed ones<br />
# git add -u -> changed and removed but not new ones.<br />
<br />
=== Amend Last Comment ===<br />
* Amend the last comment<br />
$ git commit --amend -m "New comment"<br />
<br />
== Push ==<br />
=== Add Remote ===<br />
* To view all remote<br />
<pre><br />
$ git remote -v <br />
</pre><br />
<br />
* Add a remote<br />
<pre><br />
$ git remote add origin git@git-repo:project <br />
</pre><br />
Then<br />
<pre><br />
$ git push -u origin master <br />
</pre><br />
Or<br />
<pre><br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
</pre><br />
=== Push to Remote ===<br />
* Push to the remote git repository<br />
$ git push<br />
<br />
* If it gives error messages like:<br />
No refs in common and none specified; doing nothing.<br />
Perhaps you should specify a branch such as 'master'.<br />
(It doesn't happen if you have already done <code>git push -u origin master </code>).<br />
* Do<br />
$ git push origin master<br />
<br />
== Revert ==<br />
=== Reset ===<br />
* Revert to last commit.<br />
$ git reset --hard HEAD <br />
Or to specific commit/tag/branch<br />
$ git reset --hard <tag/branch/commit id> <br />
<br />
* Check the id using git log<br />
$ git log <br />
<br />
* To push it<br />
$ git push <reponame> -f <br />
<br />
=== Check out ===<br />
* Or change the name of master branch then check out from the commit to be used and make it master<br />
<br />
# Rename the current master branch<br />
#: <pre>$ git branch -m another_name_for_master </pre> <br />
# Check out from another commit:<br />
#: <pre> $ git checkout a1b2c3a3blah </pre><br />
# Make it the new master branch:<br />
#: <pre> $ git checkout -b master </pre><br />
* If the old master is no longer needed and has to be removed, remove it by<br />
$ git branch -D another_name_for_master<br />
<br />
<br />
=== Restore Removed File ===<br />
'''This has to be tested first then should be rewritten'''.<br /><br />
Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.<br />
$ git rev-list -n 1 HEAD -- <file_path> <br />
<br />
Then checkout the version at the commit before.<br />
$ git checkout <deleting_commit>^ -- <file_path> <br />
<br />
Or in one command, if $file is the file.<br />
$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"<br />
* Reference<br />
[http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top]<br />
<br />
<br />
== Branch ==<br />
* Create branch<br />
<pre><br />
$ git branch branch_name <br />
</pre><br />
* Change to the branch just created<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
OR<br />
* Create and change<br />
<pre><br />
git checkout -b branch_name <br />
</pre><br />
<br />
* Back to master<br />
<pre><br />
$ git checkout master <br />
</pre><br />
<br />
* To push,<br />
<pre><br />
$ git push origin branch_name <br />
</pre><br />
<br />
<br />
== Tag ==<br />
=== List Tags ===<br />
<pre><br />
$ git tag <br />
</pre><br />
<br />
=== View Tag ===<br />
* To view tag data with commit details.<br />
<pre><br />
$ git show tag_name <br />
</pre><br />
<br />
=== Create Tag ===<br />
* Create Tag<br />
<pre><br />
$ git tag -a tag_name -m 'tag message: new version blah blah' <br />
</pre><br />
* Tag later <br />
<pre><br />
$ git tag -a tag_name -m 'tag message' commit_checksum <br />
</pre><br />
e.g.)<br />
<pre><br />
$ git tag -a v1.5 -m 'new version with blah blah' b015e9d <br />
</pre><br />
* To see the list of checksum use the following command.<br />
<pre><br />
$ git log --pretty=oneline <br />
</pre><br />
<br />
=== Remove Tag ===<br />
<pre><br />
$ git tag -d tag_name <br />
</pre><br />
<br />
<br />
== Merge Multiple Git Repositories into One ==<br />
If the directory structures look like this<br />
<pre><br />
parent-dir (not git repo)<br />
│<br />
├─git-repo1 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
├─git-repo2 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
└─ git-repo3 ## each is connected to its own remote git repository<br />
├─src<br />
└.git<br />
</pre><br />
<br />
To make it like<br />
<pre><br />
parent-dir (now it is git repo)<br />
├─src ## contains all the files and sub-dirs from those three repositories (git-repo1, git-repo2 and git-repo3).<br />
└.git<br />
<br />
</pre><br />
and it still keeps all the histories from all those repositories, do following instructions.<br />
<br />
* Initialize the new repository directory.<br />
<pre><br />
$ cd parent-dir <br />
$ git init <br />
</pre><br />
<br />
* Pull the old ones and remove these one by one.<br />
<pre><br />
$ git pull git-repo1 <br />
<br />
## If there is any conflict, solve it first then<br />
## commit the changes<br />
$ git add file <br />
$ git commit -a <br />
## Now pull the one had conflict issue again.<br />
$ git pull git-repo1 <br />
<br />
$ rm -rf git-repo1 <br />
<br />
$ git pull git-repo2 <br />
$ rm -rf git-repo2 <br />
<br />
$ git pull git-repo3 <br />
$ rm -rf git-repo3 <br />
</pre><br />
<br />
* Done! Add the remote repo info if necessary.<br />
<br />
= Migration from SVN to Git =<br />
== Install git-svn ==<br />
$ apt-get install git-svn <br />
<br />
== Prepare for Migration ==<br />
In some dir, create users.txt having all the user info<br />
e.g.)<br />
USERNAME = FirstName LastName <user@host><br />
<br />
== Migrate ==<br />
=== Without Tags ===<br />
$ git svn clone --stdlayout --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project-tmp <br />
(https://svn.repo/some-parent-folder/project-folder <- Not trunk)<br />
<br />
$ cd project-tmp <br />
<br />
$ git svn fetch <br />
<br />
* To view the other SVN branches<br />
$ git branch -r<br />
<br />
$ cd ..<br />
<br />
$ git clone project-tmp project <br />
$ rm -Rf project-tmp <br />
<br />
$ cd project <br />
$ git remote rm origin <br />
<br />
* Before this, add git repo for the project first.<br />
$ git remote add origin git@git-repo:project <br />
<br />
* To view all remote<br />
$ git remote -v <br />
<br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
<br />
<br />
* add user info to project/.git/config<br />
[user]<br />
email = user@host<br />
<br />
$ git push -u origin master <br />
<br />
* Check status<br />
$ git status <br />
# On branch master<br />
nothing to commit (working directory clean)<br />
<br />
<br />
=== With Tags ===<br />
If there are nested tag folders like <code>tags/released/1.x/1.1.0, tags/released/1.x/1.2.0, tags/released/1.x/1.3.0, tags/released/2.x/2.0.1 and tags/released/2.x/2.2.0</code>, add <code>--tags</code> (for branches, it's <code>--branches</code>)<br />
$ git svn clone --stdlayout --tags=tags/*/*/* --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project <br />
$ cd project<br />
$ git svn fetch <br />
<br />
<pre><br />
#### not necessary as it will be handled differently (by create tag and remove the svn tag) <br />
$ cp -Rf .git/refs/remotes/tags/* .git/refs/tags/ <br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ cp -Rf .git/refs/remotes/* .git/refs/heads/ <br />
$ rm -Rf .git/refs/remotes <br />
###################################################################################<br />
</pre><br />
<br />
$ git branch -r<br />
Then for each tag listed do:<br />
<br />
$ git tag tagname tags/tagname<br />
$ git branch -r -d tags/tagname<br />
<br />
$ git tag tag-name-for-git tags/tag-name-from-svn<br />
$ git branch -r -d tags/tag-name-from-svn<br />
<br />
e.g.)<br />
$ git tag 0.1.15 tags/released/0.x/some-app-0.1.15<br />
$ git branch -r -d tags/released/0.x/some-app-0.1.15<br />
<br />
* list tags<br />
$ git tag -l<br />
<br />
* remove old svn tags<br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ git remote add origin git@git-repo:project <br />
<br />
* add user info to project/.git/config <br />
$ git config -e<br />
<br />
[user]<br />
email = user@host<br />
<br />
and remove [svn-remote "svn"] and [svn]<br />
$ git push origin --all <br />
<br />
It doesn't seem like the tags are pushed?<br />
$ git push origin --tags</div>Kevinhttp://kevinlee.io/wiki/HAProxyHAProxy2014-11-29T17:34:54Z<p>Kevin: /* Installation */</p>
<hr />
<div>[[Category:Network]]<br />
= Installation =<br />
<pre><br />
$ apt-get install make <br />
</pre><br />
* for gcc<br />
<pre><br />
$ apt-get install build-essential <br />
</pre><br />
<br />
* If the following error occurs,<br />
<pre><br />
# Install libpcre3-dev if you get "include/common/regex.h:28:18: fatal error: pcre.h: No such file or directory"<br />
</pre><br />
* install <br />
<pre><br />
$ apt-get install libpcre3-dev <br />
</pre><br />
<br />
* If the following error occurs,<br />
<pre><br />
# libssl-dev if you get "include/types/server.h:29:25: fatal error: openssl/ssl.h: No such file or directory"<br />
</pre><br />
* install<br />
<pre><br />
$ apt-get install libssl-dev <br />
</pre><br />
<br />
* HAProxy website:<br />
<pre><br />
http://haproxy.1wt.eu/<br />
</pre><br />
<br />
== HAProxy Installation ==<br />
* Download HAProxy,<br />
e.g.)<br />
<pre><br />
$ wget http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev19.tar.gz <br />
</pre><br />
* Install<br />
<pre><br />
$ tar -zxvf haproxy-1.5-dev19.tar.gz <br />
$ cd haproxy-1.5-dev19 <br />
$ make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 <br />
$ make install <br />
</pre><br />
<br />
* Clean all for recompilation<br />
<pre><br />
$ make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 clean all <br />
</pre><br />
<br />
* Add haproxy user<br />
<pre><br />
$ useradd -m haproxy -s /bin/false <br />
</pre><br />
<br />
* For SSL support<br />
<pre><br />
$ cat server_domain-crt-bundle.crt server_domain.key > server_domain.pem <br />
</pre><br />
* Modify the <code>/etc/haproxy/haproxy.cfg</code> file<br />
<pre><br />
bind :443 ssl crt /etc/ssl/certs/server_domain.pem no-sslv3 <br />
</pre><br />
'''''* <code>no-sslv3</code> to disable SSLv3 due to the vulnerability found in SSL protocol 3.0.'''''<br />
<br />
= Configuration =<br />
== haproxy.cfg ==<br />
* Example of <code>/etc/haproxy/haproxy.cfg</code><br />
<br />
<pre><br />
global<br />
log 127.0.0.1 local0<br />
log 127.0.0.1 local1 notice<br />
#log loghost local0 info<br />
maxconn 4096<br />
#chroot /usr/share/haproxy<br />
user haproxy<br />
group haproxy<br />
daemon<br />
#debug<br />
#quiet<br />
stats socket /var/run/haproxy/haproxy.sock mode 0600 level admin<br />
<br />
defaults<br />
log global<br />
mode http<br />
option httplog<br />
option dontlognull<br />
retries 3<br />
option redispatch<br />
maxconn 2000<br />
contimeout 5000<br />
clitimeout 50000<br />
srvtimeout 50000<br />
<br />
## first.domain.com { ##<br />
<br />
# frontend public<br />
frontend http_first<br />
# HTTP<br />
bind 192.168.0.222:80<br />
<br />
# Redirect all HTTP traffic to HTTPS<br />
redirect scheme https if !{ ssl_fc }<br />
<br />
frontend https_first<br />
<br />
bind 192.168.0.222:443 ssl crt /location/to/ssl/first.pem<br />
<br />
default_backend main_backend_https<br />
<br />
backend main_backend_https<br />
mode http<br />
<br />
# Tell the backend that this is a secure connection,<br />
# even though it's getting plain HTTP.<br />
reqadd X-Forwarded-Proto:\ https<br />
<br />
# Check by hitting a page intended for this use.<br />
# option httpchk GET /isrunning<br />
option httpchk<br />
timeout check 500ms<br />
# Wait 500ms between checks.<br />
<br />
option forwardfor header X-Real-IP<br />
option http-server-close<br />
<br />
balance roundrobin<br />
cookie JSESSIONID prefix<br />
<br />
server app_backend1 192.168.0.301:80 check port 80 cookie app_backend1<br />
server app_backend2 192.168.0.302:80 check port 80 cookie app_backend2<br />
<br />
## } first.domain.com ##<br />
<br />
<br />
## second.domain.com { ##<br />
<br />
frontend http_second<br />
<br />
bind 192.168.0.202:80<br />
<br />
redirect scheme https if !{ ssl_fc }<br />
<br />
frontend https_second<br />
<br />
bind 192.168.0.202:443 ssl crt /location/to/ssl/second.pem<br />
<br />
default_backend main_backend_https<br />
<br />
## } second.domain.com ##<br />
<br />
<br />
## third.domain.com { ##<br />
<br />
frontend http_third<br />
<br />
bind 192.168.0.203:80<br />
redirect scheme https if !{ ssl_fc }<br />
<br />
frontend https_third<br />
<br />
bind 192.168.0.203:443 ssl crt /location/to/ssl/third.pem<br />
<br />
default_backend main_backend_https<br />
<br />
## } third.domain.com ##<br />
<br />
<br />
## fourth.domain.com { ##<br />
<br />
frontend http_fourth<br />
<br />
bind 192.168.0.204:80<br />
redirect scheme https if !{ ssl_fc }<br />
<br />
frontend https_fourth<br />
<br />
bind 192.168.0.204:443 ssl crt /location/to/ssl/fourth.pem<br />
<br />
default_backend main_backend_https<br />
<br />
## } fourth.domain.com ##<br />
<br />
<br />
## fifth.domain.com { ##<br />
<br />
frontend http_fifth<br />
<br />
bind 192.168.0.205:80<br />
redirect scheme https if !{ ssl_fc }<br />
<br />
frontend https_fifth<br />
<br />
bind 192.168.0.205:443 ssl crt /location/to/ssl/fifth.pem<br />
<br />
default_backend main_backend_https<br />
<br />
## } fifth.domain.com ##<br />
<br />
</pre><br />
<br />
== Log ==<br />
HAProxy uses syslog instead of writing it directly into a file.<br />
<br />
So change the configuration fine of the default syslog daemon that is rsyslogd.<br />
* Edit <code>/etc/rsyslog.conf</code> <br />
<pre><br />
$ModLoad imudp<br />
$UDPServerAddress 127.0.0.1<br />
$UDPServerRun 514<br />
</pre><br />
<br />
* Also Edit <code>/etc/rsyslog.d/49-haproxy.conf</code><br />
<pre><br />
local0.* -/var/log/haproxy_0.log<br />
local1.* -/var/log/haproxy_1.log<br />
& ~<br />
</pre><br />
<br />
* Edit <code>/etc/logrotate.d/haproxy</code><br />
<pre><br />
/var/log/haproxy*.log<br />
{<br />
rotate 1000<br />
weekly<br />
missingok<br />
notifempty<br />
compress<br />
delaycompress<br />
size 20M<br />
sharedscripts<br />
postrotate<br />
reload rsyslog >/dev/null 2>&1 || true<br />
endscript<br />
}<br />
</pre><br />
<br />
<pre><br />
$ restart rsyslog <br />
</pre><br />
<br />
= HATop =<br />
== Installation ==<br />
<pre><br />
$ apt-get install hatop <br />
</pre><br />
<br />
== Usage ==<br />
<pre><br />
$ hatop -s /var/run/haproxy/haproxy.sock <br />
</pre><br />
Or set alias<br />
<pre><br />
alias hamonitor='hatop -s /var/run/haproxy/haproxy.sock'<br />
</pre></div>Kevinhttp://kevinlee.io/wiki/HAProxyHAProxy2014-11-29T17:31:03Z<p>Kevin: /* HAProxy Installation */</p>
<hr />
<div>[[Category:Network]]<br />
= Installation =<br />
<pre><br />
$ apt-get install make <br />
</pre><br />
* for gcc<br />
<pre><br />
$ apt-get install build-essential <br />
</pre><br />
<br />
* If the following error occurs,<br />
<pre><br />
# Install libpcre3-dev if you get "include/common/regex.h:28:18: fatal error: pcre.h: No such file or directory"<br />
</pre><br />
* install <br />
<pre><br />
$ apt-get install libpcre3-dev <br />
</pre><br />
<br />
* If the following error occurs,<br />
<pre><br />
# libssl-dev if you get "include/types/server.h:29:25: fatal error: openssl/ssl.h: No such file or directory"<br />
</pre><br />
* install<br />
<pre><br />
$ apt-get install libssl-dev <br />
</pre><br />
<br />
* HAProxy website:<br />
<pre><br />
http://haproxy.1wt.eu/<br />
</pre><br />
<br />
== HAProxy Installation ==<br />
* Download HAProxy,<br />
e.g.)<br />
<pre><br />
$ wget http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev19.tar.gz <br />
</pre><br />
* Install<br />
<pre><br />
$ tar -zxvf haproxy-1.5-dev19.tar.gz <br />
$ cd haproxy-1.5-dev19 <br />
$ make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 <br />
$ make install <br />
</pre><br />
<br />
* Clean all for recompilation<br />
<pre><br />
$ make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 clean all <br />
</pre><br />
<br />
* Add haproxy user<br />
<pre><br />
$ useradd -m haproxy -s /bin/false <br />
</pre><br />
<br />
* For SSL support<br />
<pre><br />
$ cat server_domain-crt-bundle.crt server_domain.key > server_domain.pem <br />
</pre><br />
* Modify the <code>/etc/haproxy/haproxy.cfg</code> file<br />
<pre><br />
bind :443 ssl crt /etc/ssl/certs/server_domain.pem no-sslv3 <br />
</pre><br />
* <code>no-sslv3</code> to disable SSLv3 due to the vulnerability found in SSL protocol 3.0.<br />
<br />
= Configuration =<br />
== haproxy.cfg ==<br />
* Example of <code>/etc/haproxy/haproxy.cfg</code><br />
<br />
<pre><br />
global<br />
log 127.0.0.1 local0<br />
log 127.0.0.1 local1 notice<br />
#log loghost local0 info<br />
maxconn 4096<br />
#chroot /usr/share/haproxy<br />
user haproxy<br />
group haproxy<br />
daemon<br />
#debug<br />
#quiet<br />
stats socket /var/run/haproxy/haproxy.sock mode 0600 level admin<br />
<br />
defaults<br />
log global<br />
mode http<br />
option httplog<br />
option dontlognull<br />
retries 3<br />
option redispatch<br />
maxconn 2000<br />
contimeout 5000<br />
clitimeout 50000<br />
srvtimeout 50000<br />
<br />
## first.domain.com { ##<br />
<br />
# frontend public<br />
frontend http_first<br />
# HTTP<br />
bind 192.168.0.222:80<br />
<br />
# Redirect all HTTP traffic to HTTPS<br />
redirect scheme https if !{ ssl_fc }<br />
<br />
frontend https_first<br />
<br />
bind 192.168.0.222:443 ssl crt /location/to/ssl/first.pem<br />
<br />
default_backend main_backend_https<br />
<br />
backend main_backend_https<br />
mode http<br />
<br />
# Tell the backend that this is a secure connection,<br />
# even though it's getting plain HTTP.<br />
reqadd X-Forwarded-Proto:\ https<br />
<br />
# Check by hitting a page intended for this use.<br />
# option httpchk GET /isrunning<br />
option httpchk<br />
timeout check 500ms<br />
# Wait 500ms between checks.<br />
<br />
option forwardfor header X-Real-IP<br />
option http-server-close<br />
<br />
balance roundrobin<br />
cookie JSESSIONID prefix<br />
<br />
server app_backend1 192.168.0.301:80 check port 80 cookie app_backend1<br />
server app_backend2 192.168.0.302:80 check port 80 cookie app_backend2<br />
<br />
## } first.domain.com ##<br />
<br />
<br />
## second.domain.com { ##<br />
<br />
frontend http_second<br />
<br />
bind 192.168.0.202:80<br />
<br />
redirect scheme https if !{ ssl_fc }<br />
<br />
frontend https_second<br />
<br />
bind 192.168.0.202:443 ssl crt /location/to/ssl/second.pem<br />
<br />
default_backend main_backend_https<br />
<br />
## } second.domain.com ##<br />
<br />
<br />
## third.domain.com { ##<br />
<br />
frontend http_third<br />
<br />
bind 192.168.0.203:80<br />
redirect scheme https if !{ ssl_fc }<br />
<br />
frontend https_third<br />
<br />
bind 192.168.0.203:443 ssl crt /location/to/ssl/third.pem<br />
<br />
default_backend main_backend_https<br />
<br />
## } third.domain.com ##<br />
<br />
<br />
## fourth.domain.com { ##<br />
<br />
frontend http_fourth<br />
<br />
bind 192.168.0.204:80<br />
redirect scheme https if !{ ssl_fc }<br />
<br />
frontend https_fourth<br />
<br />
bind 192.168.0.204:443 ssl crt /location/to/ssl/fourth.pem<br />
<br />
default_backend main_backend_https<br />
<br />
## } fourth.domain.com ##<br />
<br />
<br />
## fifth.domain.com { ##<br />
<br />
frontend http_fifth<br />
<br />
bind 192.168.0.205:80<br />
redirect scheme https if !{ ssl_fc }<br />
<br />
frontend https_fifth<br />
<br />
bind 192.168.0.205:443 ssl crt /location/to/ssl/fifth.pem<br />
<br />
default_backend main_backend_https<br />
<br />
## } fifth.domain.com ##<br />
<br />
</pre><br />
<br />
== Log ==<br />
HAProxy uses syslog instead of writing it directly into a file.<br />
<br />
So change the configuration fine of the default syslog daemon that is rsyslogd.<br />
* Edit <code>/etc/rsyslog.conf</code> <br />
<pre><br />
$ModLoad imudp<br />
$UDPServerAddress 127.0.0.1<br />
$UDPServerRun 514<br />
</pre><br />
<br />
* Also Edit <code>/etc/rsyslog.d/49-haproxy.conf</code><br />
<pre><br />
local0.* -/var/log/haproxy_0.log<br />
local1.* -/var/log/haproxy_1.log<br />
& ~<br />
</pre><br />
<br />
* Edit <code>/etc/logrotate.d/haproxy</code><br />
<pre><br />
/var/log/haproxy*.log<br />
{<br />
rotate 1000<br />
weekly<br />
missingok<br />
notifempty<br />
compress<br />
delaycompress<br />
size 20M<br />
sharedscripts<br />
postrotate<br />
reload rsyslog >/dev/null 2>&1 || true<br />
endscript<br />
}<br />
</pre><br />
<br />
<pre><br />
$ restart rsyslog <br />
</pre><br />
<br />
= HATop =<br />
== Installation ==<br />
<pre><br />
$ apt-get install hatop <br />
</pre><br />
<br />
== Usage ==<br />
<pre><br />
$ hatop -s /var/run/haproxy/haproxy.sock <br />
</pre><br />
Or set alias<br />
<pre><br />
alias hamonitor='hatop -s /var/run/haproxy/haproxy.sock'<br />
</pre></div>Kevinhttp://kevinlee.io/wiki/PostgreSQLPostgreSQL2014-09-13T15:28:35Z<p>Kevin: /* Shared Memory Segment Issue */</p>
<hr />
<div>[[Category:Linux]]<br />
[[Category:Software Development]]<br />
[[Category:Database]]<br />
{{Infobox software<br />
| name = PostgreSQL<br />
| logo = <br />
| screenshot =<br />
| caption =<br />
| developer = PostgreSQL Global Development Group<br />
| latest release version = 9.2.2,<br />9.1.7,<br />9.0.11,<br />8.4.15,<br />8.3.22<br />
| latest release date = <br />{{Start date and age|2012|12|06}}<br />
| latest preview version = <!--Add alpha, when it becomes available--><br />
| latest preview date = <!--{{Start date and age|YYYY|MM|DD}}--><br />
| operating system = [[Cross-platform]]<br />
| programming language = [[C (programming language)|C]]<br />
| genre = [[Object-relational database|ORDBMS]]<br />
| license = PostgreSQL licence<ref>{{cite web |date=2010-02-18|title=PostgreSQL licence approved by OSI| url= http://www.crynwr.com/cgi-bin/ezmlm-cgi?17:mmp:969| publisher = Crynwr | accessdate = 2010-02-18}}</ref><ref>{{cite web | publisher = Open Source Initiative | date = 2010-02-20|title=OSI PostgreSQL licence|url=http://www.opensource.org/licenses/postgresql | accessdate = 2010-02-20}}</ref><ref>{{cite web|url= http://www.postgresql.org/about/licence|title= License| publisher = PostgreSQL Global Development Group|accessdate=2010-09-20}}</ref><br />
| website = {{URL|www.postgresql.org}}<br />
}}<br />
<br />
==PostgreSQL==<br />
[http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] is an object-relational database management system (ORDBMS). The documentation of PostgreSQL 8.4.4 can be found [http://www.postgresql.org/docs/8.4/static here]<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static | title = PostgreSQL 8.4.4 Documentation | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
==Installation==<br />
*Install PostgreSQL<br />
<pre><br />
$ sudo apt-get install postgresql <br />
</pre><br />
<br />
==Administration==<br />
=== Setup ===<br />
====Setup Authentication Methods====<br />
* In order to allow new users to log in the PostgreSQL server, <code>/etc/postgresql/8.4/main/pg_hba.conf</code> should be modified.<br />
<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all ident<br />
# IPv4 local connections:<br />
host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
host all all ::1/128 md5<br />
To<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all md5<br />
# IPv4 local connections:<br />
#host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
#host all all ::1/128 md5<br />
Allowing the local access only with username & password.<br />
<br />
<br />
==== Access Remotely ====<br />
* Edit <code>/etc/postgresql/9.1/main/postgresql.conf</code><br />
<pre><br />
listen_addresses = 'localhost,address,separated,by,comma'<br />
</pre><br />
'''The IP is the local one but NOT a remote one'''.<br />
e.g.) 192.168.0.111 means it listens any access to 192.168.0.111 (local IP).<br />
<br />
e.g.)<br />
<pre><br />
listen_addresses = 'localhost,192.168.0.111'<br />
</pre><br />
<br />
* Edit <code>/etc/postgresql/9.1/main/pg_hba.conf</code><br />
<pre><br />
host all all IP/HERE trust<br />
</pre><br />
e.g.)<br />
<pre><br />
host all all 192.168.0.121/32 trust<br />
</pre><br />
'''The IP is not the local one but a remote one'''.<br />
e.g.) 192.168.0.121 means access from 192.168.0.121 is allowed.<br />
<br />
* Test if it works.<br />
<pre><br />
# -W: force password prompt<br />
$ psql -h IP-TO-DB-SERVER -U USERNAME -W <br />
</pre><br />
e.g.)<br />
<pre><br />
$ psql -h 192.168.0.123 -U db_user -W <br />
</pre><br />
<br />
===Roles (Users)===<br />
====Setup Root User Password====<br />
*Set up the password of the PostgreSQL root user<br />
$ sudo -u postgres psql postgres <br />
<br />
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.<br />
<br />
Type: \copyright for distribution terms<br />
\h for help with SQL commands<br />
\? for help with psql commands<br />
\g or terminate with semicolon to execute query<br />
\q to quit<br />
<br />
postgres=# '''ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';''' <br />
or<br />
postgres=# '''\password postgres''' <br />
<br />
Use <code>\quit</code> or <code>\q</code> to quit psql.<br />
postgres=# \q <br />
<br />
<br />
====Create Role (User)====<br />
Creating a user in PostgreSQL is in fact creating a role. So there are two ways to do it. The first one is using <code>CREATE ROLE</code><ref>{{cite web |url=http://www.postgresql.org/docs/8.4/static/database-roles.html | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles | date = 2009 | accessdate = 2010-08-02}}</ref> SQL command and the other one is using the programme, <code>createuser</code><ref>{{cite web|url= http://www.postgresql.org/docs/8.4/static/app-createuser.html | title= PostgreSQL 8.4.4 Documentation - createuser | date = 2009 | accessdate = 2010-08-02}}</ref>.<br />
<br />
=====CREATE ROLE=====<br />
<code>CREATE ROLE</code><ref>{{cite web |url = http://www.postgresql.org/docs/8.4/static/sql-createrole.html | title = PostgreSQL 8.4.4 Documentation - CREATE ROLE | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
CREATE ROLE role_name; <br />
CREATE ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
CREATE USER role_name WITH ENCRYPTED PASSWORD 'password'; <br />
(CREATE USER == CREATE ROLE + LOGIN) <br />
<br />
'''Usually use this.'''<br />
CREATE ROLE "role_name" WITH LOGIN ENCRYPTED PASSWORD 'password';<br />
<br />
=====createuser=====<br />
createuser [option...] [username] <br />
<br />
So to create a new user and a db belonging to him/her.<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-P: it will ask you to enter the password for the new user ('''role''').<br />
<br />
<br />
* Options<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createuser.html#AEN67555 | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles#Options | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
createuser accepts the following command-line arguments:<br />
<br />
username<br />
<br />
Specifies the name of the PostgreSQL user to be created. This name must be different from all existing roles in this PostgreSQL installation. <br />
-s<br />
--superuser<br />
<br />
The new user will be a superuser. <br />
-S<br />
--no-superuser<br />
<br />
The new user will not be a superuser. This is the default. <br />
-d<br />
--createdb<br />
<br />
The new user will be allowed to create databases. <br />
-D<br />
--no-createdb<br />
<br />
The new user will not be allowed to create databases. This is the default. <br />
-r<br />
--createrole<br />
<br />
The new user will be allowed to create new roles (that is, this user will have CREATEROLE privilege). <br />
-R<br />
--no-createrole<br />
<br />
The new user will not be allowed to create new roles. This is the default. <br />
-l<br />
--login<br />
<br />
The new user will be allowed to log in (that is, the user name can be used as the initial session user identifier). This is the default. <br />
-L<br />
--no-login<br />
<br />
The new user will not be allowed to log in. (A role without login privilege is still useful as a means of managing database permissions.) <br />
-i<br />
--inherit<br />
<br />
The new role will automatically inherit privileges of roles it is a member of. This is the default. <br />
-I<br />
--no-inherit<br />
<br />
The new role will not automatically inherit privileges of roles it is a member of. <br />
-c number<br />
--connection-limit number<br />
<br />
Set a maximum number of connections for the new user. The default is to set no limit. <br />
-P<br />
--pwprompt<br />
<br />
If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication. <br />
-E<br />
--encrypted<br />
<br />
Encrypts the user's password stored in the database. If not specified, the default password behavior is used. <br />
-N<br />
--unencrypted<br />
<br />
Does not encrypt the user's password stored in the database. If not specified, the default password behavior is used. <br />
-e<br />
--echo<br />
<br />
Echo the commands that createuser generates and sends to the server. <br />
<br />
You will be prompted for a name and other missing information if it is not specified on the command line.<br />
<br />
createuser also accepts the following command-line arguments for connection parameters:<br />
<br />
-h host<br />
--host host<br />
<br />
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. <br />
-p port<br />
--port port<br />
<br />
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. <br />
-U username<br />
--username username<br />
<br />
User name to connect as (not the user name to create). <br />
-w<br />
--no-password<br />
<br />
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. <br />
-W<br />
--password<br />
<br />
Force createuser to prompt for a password (for connecting to the server, not for the password of the new user).<br />
<br />
This option is never essential, since createuser will automatically prompt for a password if the server demands password authentication. However, createuser will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt. <br />
<br />
<br />
====View Roles====<br />
<br />
* To see all the exising roles.<br />
SELECT * FROM pg_roles;<br />
<br />
* To see the details of an existing role<br />
SELECT role_name FROM pg_roles; <br />
<br />
<br />
====Change Role====<br />
* <code>ALTER ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-alterrole.html | title = PostgreSQL 8.4.4 Documentation - ALTER ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
ALTER ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
<br />
<br />
====Remove Role====<br />
* Using <code>DROP ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-droprole.html | title = PostgreSQL 8.4.4 Documentation - DROP ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
DROP ROLE role_name; <br />
<br />
*Using <code>dropuser</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-dropuser.html | title = PostgreSQL 8.4.4 Documentation - dropuser | date = 2009 | accessdate = 2010-08-02}}</ref><br />
dropuser name <br />
<br />
===Databases===<br />
====List Databases====<br />
postgres=# \l <br />
<br />
====Select (Change) Database====<br />
postgres=# \c DB_NAME <br />
<br />
====List Table====<br />
<br />
postgres=# \d <br />
<br />
====Display Table Description====<br />
<br />
postgres=# \d TABLE_NAME <br />
<br />
<br />
====Creating Database====<br />
* Using <code>CREATE DATABASE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-createdatabase.html | title = PostgreSQL 8.4.4 Documentation - CREATE DATABASE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL to create a new database.<br />
CREATE DATABASE "db_name" <br />
<br />
CREATE DATABASE "db_name" OWNER "role_name" <br />
<br />
* Similar to creating a role, it is also possible to create a new database from the shell by using <code>createdb</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createdb.html | title = PostgreSQL 8.4.4 Documentation - createdb | date = 2009 | accessdate = 2010-08-02}}</ref> command.<br />
createdb [option...] [db_name] [description]<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-O owner<br />
--owner owner<br />
Specifies the database user who will own the new database.<br />
<br />
====Change Database====<br />
e.g.)<br />
postgres=# ALTER DATABASE db_name OWNER TO new_owner; <br />
<br />
====Privileges====<br />
=====GRANT=====<br />
e.g.)<br />
postgres=# GRANT ALL PRIVILEGES ON DATABASE db_name TO username; <br />
<br />
<br />
====Drop and Recreate database====<br />
* Access db<br />
$ sudo -u postgres psql postgres <br />
And drop and recreate the database.<br />
DROP DATABASE DB_NAME; <br />
CREATE DATABASE DB_NAME OWNER ROLE_NAME; <br />
<br />
====Back Up Database====<br />
$ sudo -u postgres pg_dump DB_NAME > DB_DUMP_DB_NAME.sql <br />
<br />
====Restore Database====<br />
$ sudo -u postgres psql --set ON_ERROR_STOP=on DB_NAME < DB_DUMP_DB_NAME.sql <br />
<br />
<br />
====Run SQL Script====<br />
* Use psql terminal and select DB.<br />
$ sudo -u postgres psql <br />
postgres=# \c DB_NAME <br />
OR Access the DB directly<br />
$ sudo -u postgres psql DB_NAME <br />
DB_NAME=# <br />
<br />
* Run SQL script from within psql<br />
DB_NAME=# \set ON_ERROR_STOP <br />
DB_NAME=# \i db.sql<br />
<br />
* Run SQL query<br />
DB_NAME=# type SQL; <br />
e.g.)<br />
DB_NAME=# SELECT * FROM some_table;<br />
<br />
====Store the Query Result in File====<br />
DB_NAME=# \o file_name <br />
# Run some query<br />
DB_NAME=# SELECT * FROM some_table; <br />
# \o to finish<br />
DB_NAME=# \o<br />
==== Store Query Result in CSV file ====<br />
<pre><br />
\copy (SELECT * FROM products WHERE product_name LIKE 'SomePreFix%') To '/path/to/store/csv-file.csv' With CSV HEADER; <br />
</pre><br />
<br />
====Terminate any processing user of database====<br />
* To terminate all the processing users of the database, run the following script.<br />
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='DB_NAME';<br />
<br />
<br />
<br />
===Restart Server===<br />
* To restart the server, /etc/init.d/postgresql-8.x (depending on the version).<br />
sudo /etc/init.d/postgresql-8.4 restart<br />
<br />
==Install pgAdmin III==<br />
<pre><br />
$ sudo apt-get install pgadmin3 <br />
</pre><br />
<br />
==Troubleshooting==<br />
===Shared Memory Segment Issue===<br />
* http://askubuntu.com/questions/44373/how-to-fix-postgresql-installation<br />
* http://leopard.in.ua/2013/09/05/postgresql-sessting-shared-memory<br />
<br />
==References==<br />
{{Reflist|1}}</div>Kevinhttp://kevinlee.io/wiki/PostgreSQLPostgreSQL2014-09-13T14:49:33Z<p>Kevin: </p>
<hr />
<div>[[Category:Linux]]<br />
[[Category:Software Development]]<br />
[[Category:Database]]<br />
{{Infobox software<br />
| name = PostgreSQL<br />
| logo = <br />
| screenshot =<br />
| caption =<br />
| developer = PostgreSQL Global Development Group<br />
| latest release version = 9.2.2,<br />9.1.7,<br />9.0.11,<br />8.4.15,<br />8.3.22<br />
| latest release date = <br />{{Start date and age|2012|12|06}}<br />
| latest preview version = <!--Add alpha, when it becomes available--><br />
| latest preview date = <!--{{Start date and age|YYYY|MM|DD}}--><br />
| operating system = [[Cross-platform]]<br />
| programming language = [[C (programming language)|C]]<br />
| genre = [[Object-relational database|ORDBMS]]<br />
| license = PostgreSQL licence<ref>{{cite web |date=2010-02-18|title=PostgreSQL licence approved by OSI| url= http://www.crynwr.com/cgi-bin/ezmlm-cgi?17:mmp:969| publisher = Crynwr | accessdate = 2010-02-18}}</ref><ref>{{cite web | publisher = Open Source Initiative | date = 2010-02-20|title=OSI PostgreSQL licence|url=http://www.opensource.org/licenses/postgresql | accessdate = 2010-02-20}}</ref><ref>{{cite web|url= http://www.postgresql.org/about/licence|title= License| publisher = PostgreSQL Global Development Group|accessdate=2010-09-20}}</ref><br />
| website = {{URL|www.postgresql.org}}<br />
}}<br />
<br />
==PostgreSQL==<br />
[http://en.wikipedia.org/wiki/PostgreSQL PostgreSQL] is an object-relational database management system (ORDBMS). The documentation of PostgreSQL 8.4.4 can be found [http://www.postgresql.org/docs/8.4/static here]<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static | title = PostgreSQL 8.4.4 Documentation | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
==Installation==<br />
*Install PostgreSQL<br />
<pre><br />
$ sudo apt-get install postgresql <br />
</pre><br />
<br />
==Administration==<br />
=== Setup ===<br />
====Setup Authentication Methods====<br />
* In order to allow new users to log in the PostgreSQL server, <code>/etc/postgresql/8.4/main/pg_hba.conf</code> should be modified.<br />
<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all ident<br />
# IPv4 local connections:<br />
host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
host all all ::1/128 md5<br />
To<br />
# TYPE DATABASE USER CIDR-ADDRESS METHOD<br />
<br />
# "local" is for Unix domain socket connections only<br />
local all all md5<br />
# IPv4 local connections:<br />
#host all all 127.0.0.1/32 md5<br />
# IPv6 local connections:<br />
#host all all ::1/128 md5<br />
Allowing the local access only with username & password.<br />
<br />
<br />
==== Access Remotely ====<br />
* Edit <code>/etc/postgresql/9.1/main/postgresql.conf</code><br />
<pre><br />
listen_addresses = 'localhost,address,separated,by,comma'<br />
</pre><br />
'''The IP is the local one but NOT a remote one'''.<br />
e.g.) 192.168.0.111 means it listens any access to 192.168.0.111 (local IP).<br />
<br />
e.g.)<br />
<pre><br />
listen_addresses = 'localhost,192.168.0.111'<br />
</pre><br />
<br />
* Edit <code>/etc/postgresql/9.1/main/pg_hba.conf</code><br />
<pre><br />
host all all IP/HERE trust<br />
</pre><br />
e.g.)<br />
<pre><br />
host all all 192.168.0.121/32 trust<br />
</pre><br />
'''The IP is not the local one but a remote one'''.<br />
e.g.) 192.168.0.121 means access from 192.168.0.121 is allowed.<br />
<br />
* Test if it works.<br />
<pre><br />
# -W: force password prompt<br />
$ psql -h IP-TO-DB-SERVER -U USERNAME -W <br />
</pre><br />
e.g.)<br />
<pre><br />
$ psql -h 192.168.0.123 -U db_user -W <br />
</pre><br />
<br />
===Roles (Users)===<br />
====Setup Root User Password====<br />
*Set up the password of the PostgreSQL root user<br />
$ sudo -u postgres psql postgres <br />
<br />
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.<br />
<br />
Type: \copyright for distribution terms<br />
\h for help with SQL commands<br />
\? for help with psql commands<br />
\g or terminate with semicolon to execute query<br />
\q to quit<br />
<br />
postgres=# '''ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';''' <br />
or<br />
postgres=# '''\password postgres''' <br />
<br />
Use <code>\quit</code> or <code>\q</code> to quit psql.<br />
postgres=# \q <br />
<br />
<br />
====Create Role (User)====<br />
Creating a user in PostgreSQL is in fact creating a role. So there are two ways to do it. The first one is using <code>CREATE ROLE</code><ref>{{cite web |url=http://www.postgresql.org/docs/8.4/static/database-roles.html | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles | date = 2009 | accessdate = 2010-08-02}}</ref> SQL command and the other one is using the programme, <code>createuser</code><ref>{{cite web|url= http://www.postgresql.org/docs/8.4/static/app-createuser.html | title= PostgreSQL 8.4.4 Documentation - createuser | date = 2009 | accessdate = 2010-08-02}}</ref>.<br />
<br />
=====CREATE ROLE=====<br />
<code>CREATE ROLE</code><ref>{{cite web |url = http://www.postgresql.org/docs/8.4/static/sql-createrole.html | title = PostgreSQL 8.4.4 Documentation - CREATE ROLE | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
CREATE ROLE role_name; <br />
CREATE ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
CREATE USER role_name WITH ENCRYPTED PASSWORD 'password'; <br />
(CREATE USER == CREATE ROLE + LOGIN) <br />
<br />
'''Usually use this.'''<br />
CREATE ROLE "role_name" WITH LOGIN ENCRYPTED PASSWORD 'password';<br />
<br />
=====createuser=====<br />
createuser [option...] [username] <br />
<br />
So to create a new user and a db belonging to him/her.<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-P: it will ask you to enter the password for the new user ('''role''').<br />
<br />
<br />
* Options<ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createuser.html#AEN67555 | title = PostgreSQL 8.4.4 Documentation - 20.1. Database Roles#Options | date = 2009 | accessdate = 2010-08-02}}</ref><br />
<br />
createuser accepts the following command-line arguments:<br />
<br />
username<br />
<br />
Specifies the name of the PostgreSQL user to be created. This name must be different from all existing roles in this PostgreSQL installation. <br />
-s<br />
--superuser<br />
<br />
The new user will be a superuser. <br />
-S<br />
--no-superuser<br />
<br />
The new user will not be a superuser. This is the default. <br />
-d<br />
--createdb<br />
<br />
The new user will be allowed to create databases. <br />
-D<br />
--no-createdb<br />
<br />
The new user will not be allowed to create databases. This is the default. <br />
-r<br />
--createrole<br />
<br />
The new user will be allowed to create new roles (that is, this user will have CREATEROLE privilege). <br />
-R<br />
--no-createrole<br />
<br />
The new user will not be allowed to create new roles. This is the default. <br />
-l<br />
--login<br />
<br />
The new user will be allowed to log in (that is, the user name can be used as the initial session user identifier). This is the default. <br />
-L<br />
--no-login<br />
<br />
The new user will not be allowed to log in. (A role without login privilege is still useful as a means of managing database permissions.) <br />
-i<br />
--inherit<br />
<br />
The new role will automatically inherit privileges of roles it is a member of. This is the default. <br />
-I<br />
--no-inherit<br />
<br />
The new role will not automatically inherit privileges of roles it is a member of. <br />
-c number<br />
--connection-limit number<br />
<br />
Set a maximum number of connections for the new user. The default is to set no limit. <br />
-P<br />
--pwprompt<br />
<br />
If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication. <br />
-E<br />
--encrypted<br />
<br />
Encrypts the user's password stored in the database. If not specified, the default password behavior is used. <br />
-N<br />
--unencrypted<br />
<br />
Does not encrypt the user's password stored in the database. If not specified, the default password behavior is used. <br />
-e<br />
--echo<br />
<br />
Echo the commands that createuser generates and sends to the server. <br />
<br />
You will be prompted for a name and other missing information if it is not specified on the command line.<br />
<br />
createuser also accepts the following command-line arguments for connection parameters:<br />
<br />
-h host<br />
--host host<br />
<br />
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. <br />
-p port<br />
--port port<br />
<br />
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. <br />
-U username<br />
--username username<br />
<br />
User name to connect as (not the user name to create). <br />
-w<br />
--no-password<br />
<br />
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. <br />
-W<br />
--password<br />
<br />
Force createuser to prompt for a password (for connecting to the server, not for the password of the new user).<br />
<br />
This option is never essential, since createuser will automatically prompt for a password if the server demands password authentication. However, createuser will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt. <br />
<br />
<br />
====View Roles====<br />
<br />
* To see all the exising roles.<br />
SELECT * FROM pg_roles;<br />
<br />
* To see the details of an existing role<br />
SELECT role_name FROM pg_roles; <br />
<br />
<br />
====Change Role====<br />
* <code>ALTER ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-alterrole.html | title = PostgreSQL 8.4.4 Documentation - ALTER ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
ALTER ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'password'; <br />
<br />
<br />
====Remove Role====<br />
* Using <code>DROP ROLE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-droprole.html | title = PostgreSQL 8.4.4 Documentation - DROP ROLE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL<br />
DROP ROLE role_name; <br />
<br />
*Using <code>dropuser</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-dropuser.html | title = PostgreSQL 8.4.4 Documentation - dropuser | date = 2009 | accessdate = 2010-08-02}}</ref><br />
dropuser name <br />
<br />
===Databases===<br />
====List Databases====<br />
postgres=# \l <br />
<br />
====Select (Change) Database====<br />
postgres=# \c DB_NAME <br />
<br />
====List Table====<br />
<br />
postgres=# \d <br />
<br />
====Display Table Description====<br />
<br />
postgres=# \d TABLE_NAME <br />
<br />
<br />
====Creating Database====<br />
* Using <code>CREATE DATABASE</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/sql-createdatabase.html | title = PostgreSQL 8.4.4 Documentation - CREATE DATABASE | date = 2009 | accessdate = 2010-08-02}}</ref> SQL to create a new database.<br />
CREATE DATABASE "db_name" <br />
<br />
CREATE DATABASE "db_name" OWNER "role_name" <br />
<br />
* Similar to creating a role, it is also possible to create a new database from the shell by using <code>createdb</code><ref>{{cite web | url = http://www.postgresql.org/docs/8.4/static/app-createdb.html | title = PostgreSQL 8.4.4 Documentation - createdb | date = 2009 | accessdate = 2010-08-02}}</ref> command.<br />
createdb [option...] [db_name] [description]<br />
<br />
sudo -u postgres createuser -P username <br />
sudo -u postgres createdb -O username db_name <br />
<br />
-O owner<br />
--owner owner<br />
Specifies the database user who will own the new database.<br />
<br />
====Change Database====<br />
e.g.)<br />
postgres=# ALTER DATABASE db_name OWNER TO new_owner; <br />
<br />
====Privileges====<br />
=====GRANT=====<br />
e.g.)<br />
postgres=# GRANT ALL PRIVILEGES ON DATABASE db_name TO username; <br />
<br />
<br />
====Drop and Recreate database====<br />
* Access db<br />
$ sudo -u postgres psql postgres <br />
And drop and recreate the database.<br />
DROP DATABASE DB_NAME; <br />
CREATE DATABASE DB_NAME OWNER ROLE_NAME; <br />
<br />
====Back Up Database====<br />
$ sudo -u postgres pg_dump DB_NAME > DB_DUMP_DB_NAME.sql <br />
<br />
====Restore Database====<br />
$ sudo -u postgres psql --set ON_ERROR_STOP=on DB_NAME < DB_DUMP_DB_NAME.sql <br />
<br />
<br />
====Run SQL Script====<br />
* Use psql terminal and select DB.<br />
$ sudo -u postgres psql <br />
postgres=# \c DB_NAME <br />
OR Access the DB directly<br />
$ sudo -u postgres psql DB_NAME <br />
DB_NAME=# <br />
<br />
* Run SQL script from within psql<br />
DB_NAME=# \set ON_ERROR_STOP <br />
DB_NAME=# \i db.sql<br />
<br />
* Run SQL query<br />
DB_NAME=# type SQL; <br />
e.g.)<br />
DB_NAME=# SELECT * FROM some_table;<br />
<br />
====Store the Query Result in File====<br />
DB_NAME=# \o file_name <br />
# Run some query<br />
DB_NAME=# SELECT * FROM some_table; <br />
# \o to finish<br />
DB_NAME=# \o<br />
==== Store Query Result in CSV file ====<br />
<pre><br />
\copy (SELECT * FROM products WHERE product_name LIKE 'SomePreFix%') To '/path/to/store/csv-file.csv' With CSV HEADER; <br />
</pre><br />
<br />
====Terminate any processing user of database====<br />
* To terminate all the processing users of the database, run the following script.<br />
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='DB_NAME';<br />
<br />
<br />
<br />
===Restart Server===<br />
* To restart the server, /etc/init.d/postgresql-8.x (depending on the version).<br />
sudo /etc/init.d/postgresql-8.4 restart<br />
<br />
==Install pgAdmin III==<br />
<pre><br />
$ sudo apt-get install pgadmin3 <br />
</pre><br />
<br />
==Troubleshooting==<br />
===Shared Memory Segment Issue===<br />
http://askubuntu.com/questions/44373/how-to-fix-postgresql-installation<br />
<br />
<br />
==References==<br />
{{Reflist|1}}</div>Kevinhttp://kevinlee.io/wiki/Apache_TomcatApache Tomcat2014-05-10T21:19:55Z<p>Kevin: /* KeyStore */</p>
<hr />
<div>[[Category:Web Application Development (Java)]][[Category:Server]]<br />
==Apache Tomcat==<br />
===Installation===<br />
-Download and extract the file<br />
<pre><br />
$ sudo tar -zxvf apache-tomcat-7.0.34.tar.gz <br />
</pre><br />
<br />
-create user <code>tomcat</code><br />
<pre><br />
$ sudo useradd -d /opt/tomcat_user_home tomcat -s /bin/bash <br />
<br />
# OR this would be better.<br />
$ sudo adduser --disabled-login --gecos 'Tomcat' --home /opt/tomcat_user_home tomcat <br />
</pre><br />
<br />
<pre><br />
$ sudo passwd tomcat <br />
<br />
Enter new UNIX password:<br />
Retype new UNIX password:<br />
passwd: password updated successfully<br />
</pre><br />
<br />
<pre><br />
$ chown -R tomcat:tomcat /opt/tomcat_user_home <br />
</pre><br />
<br />
<pre><br />
$ sudo chown -R tomcat:tomcat apache-tomcat-7.0.33 <br />
</pre><br />
<br />
<pre><br />
$ sudo ln -s apache-tomcat-7.0.33/ tomcat <br />
</pre><br />
<br />
-To run<br />
$ su - tomcat <br />
Password: '''type tomcat password'''<br />
$ cd /opt/tomcat/bin <br />
/opt/tomcat/bin$ ./catalina.sh start<br />
<br />
<br />
-To automatically start when the computer is boot.<br />
<pre><br />
$ sudo ln -s /opt/tomcat/bin/catalina.sh /etc/init.d/tomcat <br />
</pre><br />
-or it might be better to have a tomcat start and stop script with a proper $JAVA_HOME set.<br />
To do this create <code>tomcat.sh</code> file in the <code>bin</code> directory. (Make sure that there is no <code>tomcat.sh</code> in the <code>bin</code> directory).<br />
<pre><br />
$ cd /opt/tomcat/bin <br />
$ vim tomcat.sh <br />
</pre><br />
<pre><br />
#!/bin/sh<br />
<br />
### BEGIN INIT INFO<br />
# Provides: tomcat<br />
# Required-Start: $network<br />
# Required-Stop: $network<br />
# Default-Start: 2 3 4 5<br />
# Default-Stop: 0 1 6<br />
# Short-Description: Start/Stop Tomcat server<br />
### END INIT INFO<br />
<br />
<br />
# export JAVA_HOME=/usr/lib/jvm/java-6-sun<br />
# export JAVA_HOME=/usr/lib/jvm/java-6-openjdk<br />
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk<br />
# export JAVA_OPTS="-server -Xms64m -Xmx192m -XX:MaxPermSize=256m -XX:+DisableExplicitGC -XX:-DoEscapeAnalysis"<br />
export JAVA_OPTS="-server -Xms512m -Xmx768m -XX:MaxPermSize=384m -XX:+DisableExplicitGC -XX:-DoEscapeAnalysis"<br />
<br />
PRG="$0"<br />
<br />
while [ -h "$PRG" ]; do<br />
ls=`ls -ld "$PRG"`<br />
link=`expr "$ls" : '.*-> \(.*\)$'`<br />
if expr "$link" : '/.*' > /dev/null; then<br />
PRG="$link"<br />
else<br />
PRG=`dirname "$PRG"`/"$link"<br />
fi<br />
done<br />
<br />
# Get standard environment variables<br />
PRGDIR=`dirname "$PRG"`<br />
<br />
# Only set CATALINA_HOME if not already set<br />
[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`<br />
<br />
cd /opt/tomcat_user_home<br />
/bin/su tomcat $CATALINA_HOME/bin/catalina.sh $1<br />
<br />
</pre><br />
* If <code>/dev/urandom</code> should be used instead of <code>/dev/random</code> add <code>-Djava.security.egd=file:/dev/./urandom</code> to <code>JAVA_OPTS</code>.<br />
e.g.)<br />
<pre><br />
export JAVA_OPTS="-server -Xms512m -Xmx768m -XX:MaxPermSize=384m -XX:+DisableExplicitGC -XX:-DoEscapeAnalysis -Djava.security.egd=file:/dev/./urandom"<br />
</pre><br />
<br />
<br />
-Make it executable (This script does not require tomcat user login to run the tomcat server. Instead, It will ask the tomcat user password when running the script).<br />
<pre><br />
$ chmod a+x tomcat.sh <br />
</pre><br />
<br />
-put the symbolic link for the automatic start.<br />
<pre><br />
$ sudo ln -s /opt/tomcat/bin/tomcat.sh /etc/init.d/tomcat <br />
</pre><br />
<br />
<pre><br />
$ sudo chmod 755 /etc/init.d/tomcat <br />
</pre><br />
<br />
Then to make it automatically start and stop when the server boots up and shuts down respectively.<br />
<pre><br />
sudo ln -s /etc/init.d/tomcat /etc/rc0.d/K10tomcat <br />
sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K10tomcat <br />
sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S90tomcat <br />
sudo ln -s /etc/init.d/tomcat /etc/rc3.d/S90tomcat <br />
sudo ln -s /etc/init.d/tomcat /etc/rc4.d/S90tomcat <br />
sudo ln -s /etc/init.d/tomcat /etc/rc5.d/S90tomcat <br />
sudo ln -s /etc/init.d/tomcat /etc/rc6.d/K10tomcat <br />
</pre><br />
OR<br />
<pre><br />
$ cd /etc/init.d <br />
$ update-rc.d tomcat defaults <br />
</pre><br />
<br />
<br />
==== References ====<br />
http://linux-sxs.org/internet_serving/c140.html<br />
<br />
http://www.linux.org/docs/ldp/howto/MMBase-Inst-HOWTO/x321.html<br />
<br />
http://www.howtogeek.com/howto/linux/installing-tomcat-6-on-ubuntu/<br />
<br />
http://www.jguru.com/faq/view.jsp?EID=425628<br />
<br />
=== Configuration ===<br />
<br />
==== Tomcat User Configuration ====<br />
* To create an encrypted password,<br />
<pre><br />
$ cd /tomcat/bin <br />
$ ./digest.sh -a SHA your_password <br />
</pre><br />
<br />
your_password:'''564e340cd48437d2dfe876ee154cc99dc4d0d137'''<br />
<br />
<br />
* Add a tomcat manager login info to the <code>/opt/tomcat/conf/tomcat-users.xml</code> file.<br />
<pre><br />
$ vim /opt/tomcat/conf/tomcat-users.xml <br />
</pre><br />
<source lang="xml"><br />
<?xml version='1.0' encoding='utf-8'?><br />
<tomcat-users><br />
<role rolename="manager"/><br />
<user username="managerid" password="564e340cd48437d2dfe876ee154cc99dc4d0d137" roles="manager"/><br />
</tomcat-users><br />
</source><br />
<br />
* Add the following Realm element in the <code>localhost</code> Host element<br />
<pre><br />
<Realm className="org.apache.catalina.realm.MemoryRealm" digest="SHA" /><br />
</pre><br />
<br />
* So it should be like this.<br />
<source lang="xml"><br />
<Host name="localhost" appBase="webapps"<br />
unpackWARs="true" autoDeploy="true"<br />
xmlValidation="false" xmlNamespaceAware="false"><br />
<br />
<Realm className="org.apache.catalina.realm.MemoryRealm" digest="SHA" /><br />
<br />
<br />
...<br />
<br />
</Host><br />
</source><br />
<br />
* Restart the tomcat server.<br />
<br />
<br />
<br />
==Forward Request from Apache Web Server to Tomcat==<br />
=== Using <code>mod_jk</code> ===<br />
==== Installation ====<br />
<pre><br />
$ sudo apt-get install libapache2-mod-jk <br />
</pre><br />
*Assumption: Apache web server is already installed.<br />
<br />
-Reload config<br />
<pre><br />
$ sudo /etc/init.d/apache2 force-reload <br />
</pre><br />
<br />
==== Configuration ====<br />
-Check if mod_jk is enabled then edit <code>/etc/apache2/mods-enabled/jk.load</code><br />
<br />
LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so<br />
<br />
'''JkWorkersFile /etc/apache2/workers.properties'''<br />
'''JkLogFile /var/log/apache2/mod_jk.log'''<br />
'''JkLogLevel debug'''<br />
'''JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "'''<br />
<br />
'''JkMount /your_app worker1'''<br />
'''JkMount /your_app/* worker1'''<br />
<br />
-Create <code>workers.properties</code> file in the <code>/etc/apache2/</code> directory.<br />
<pre><br />
workers.tomcat_home=/opt/tomcat<br />
workers.java_home=/usr/lib/jvm/java-6-sun<br />
ps=/<br />
worker.list=worker1<br />
worker.worker1.port=8009<br />
worker.worker1.host=localhost<br />
worker.worker1.type=ajp13<br />
worker.worker1.lbfactor=1<br />
</pre><br />
<br />
-Restart Apache<br />
<pre><br />
$ sudo /etc/init.d/apache2 restart <br />
</pre><br />
<br />
-Now run Tomcat and test it<br />
<pre><br />
go to <br />
http://localhost/your_app<br />
</pre><br />
<br />
<br />
* If a virtual host should handle the request, set <code>JkMount</code>, <code>JkUnMount</code> and <code>JkMountfile</code> in the virtual host.<br />
<pre><br />
<VirtualHost *:80><br />
...<br />
<br />
JkMount /myapp worker1<br />
JkMount /myapp/* worker1<br />
</VirtualHost><br />
</pre><br />
<br />
<br />
<br />
==== References ====<br />
http://ubuntuforums.org/showthread.php?t=219985<br />
<br />
http://tomcat.apache.org/connectors-doc/index.html<br />
<br />
http://tomcat.apache.org/connectors-doc/reference/uriworkermap.html<br />
<br />
http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html<br />
<br />
http://tomcat.apache.org/connectors-doc/reference/apache.html<br />
<br />
http://tomcat.apache.org/connectors-doc/reference/workers.html<br />
<br />
http://swik.net/Tomcat+Apache?popular<br />
<br />
<br />
<br />
== Link Sub-domain Directly to Application ==<br />
=== Using <code>mod_jk</code> ===<br />
* Open the <code>workers.properties</code> file in the <code>/etc/apache2</code> directory.<br />
* Add another <code>worker</code> information.<br />
workers.tomcat_home=/opt/tomcat<br />
workers.java_home=/usr/lib/jvm/java-6-sun<br />
ps=/<br />
worker.list=worker1,'''worker2'''<br />
worker.worker1.port=8009<br />
worker.worker1.host=localhost<br />
worker.worker1.type=ajp13<br />
worker.worker1.lbfactor=1<br />
'''worker.worker2.port=8009'''<br />
'''worker.worker2.host=subdomain.yourdomain.com'''<br />
'''worker.worker2.type=ajp13'''<br />
'''worker.worker2.lbfactor=1'''<br />
<br />
* Set up <code>JkMount</code> in the apache virtual host configuration.<br />
<VirtualHost *:80><br />
ServerAdmin master@yourdomain.com<br />
<br />
ServerName subdomain.yourdomain.com<br />
<br />
JkMount / worker2<br />
JkMount /* worker2<br />
<br />
</VirtualHost><br />
<br />
* Open the <code>$CATALINA_HOME/conf/server.xml</code> file to set up a tomcat virtual host.<br />
* Add a new virtual host info inside the <code>Engine</code> element.<br />
<source lang="xml"><br />
<Engine name="Catalina" defaultHost="localhost"><br />
... Default Host Info ...<br />
<br />
<Host name="subdomain.yourdomain.com" appBase="/opt/some_path/webapps"<br />
unpackWARs="true" autoDeploy="true"<br />
xmlValidation="false" xmlNamespaceAware="false"><br />
<!-- if necessary<br />
<Context path="" docBase="application_path" debug="0" reloadable="true" /><br />
--><br />
</Host><br />
</Engine><br />
</source><br />
* OPTIONAL: If the context information which is, in the example, the part commented out is set, the <code>application_path</code> must exist in the <code>appBase</code> directory which is ,in this example, <code>/opt/some_path/webapps</code> directory.<br />
/opt/some_path/webapps/application_path<br />
<br />
* Restart Tomcat and Apache.<br />
$ /etc/init.d/tomcat stop <br />
$ /etc/init.d/tomcat start <br />
$ /etc/init.d/apache restart <br />
<br />
* access http://subdomain.yourdomain.com.<br />
<br />
<br />
<br />
==Realm Configuration==<br />
<source lang="xml"><br />
<Resource name="jdbc/eVideoDataSource" auth="Container" type="javax.sql.DataSource"<br />
maxActive="2" maxIdle="1" maxWait="180"<br />
username="userId" password="password" driverClassName="com.mysql.jdbc.Driver"<br />
url="jdbc:mysql://localhost:3306/db_name?autoReconnect=true"/><br />
<br />
<br />
<Realm className="org.apache.catalina.realm.DataSourceRealm" debug="99" <br />
dataSourceName="jdbc/eVideoDataSource" localDataSource="true"<br />
userTable="login" userNameCol="username" userCredCol="password" digest="SHA-1" <br />
userRoleTable="user_roles" roleNameCol="role_name" allRolesMode="strict" /><br />
</source><br />
<br />
allRolesMode attribute can be one of "'''strict'''" or "'''authOnly'''" or "'''strictAuthOnly'''".<br />
If there is no allRolesMode specified, it will be "strict" by default.<br />
<br />
<br />
-The following is the part of RealmBase class source code from the Apache Tomcat server 5.5.25.<br />
<source lang="java5"><br />
/**<br />
* Use the strict servlet spec interpretation which requires that the user<br />
* have one of the web-app/security-role/role-name <br />
*/<br />
public static final AllRolesMode STRICT_MODE = new AllRolesMode("strict");<br />
/**<br />
* Allow any authenticated user<br />
*/<br />
public static final AllRolesMode AUTH_ONLY_MODE = new AllRolesMode("authOnly");<br />
/**<br />
* Allow any authenticated user only if there are no web-app/security-roles<br />
*/<br />
public static final AllRolesMode STRICT_AUTH_ONLY_MODE = new AllRolesMode("strictAuthOnly");<br />
</source><br />
<br />
== SSL ==<br />
=== KeyStore ===<br />
Create a folder to store the keystore file.<br />
<source lang="bash"><br />
$ mkdir ~/.tomcat_ssl <br />
</source><br />
<br />
Create a keystore file using Java's keytool<br />
<source lang="bash"><br />
$ cd ~/.tomcat_ssl <br />
$ keytool -genkey -keysize 2048 -keyalg RSA -sigalg SHA1withRSA -validity 360 -alias tomcat -keystore .tomcatKeyStore <br />
</source><br />
<code>'''.tomcatKeyStore'''</code> is the keysotre file name so change it to whatever you like.<br />
<br />
e.g.)<br />
----<br />
$ keytool -genkey -keysize 2048 -keyalg RSA -sigalg SHA1withRSA -validity 360 -alias tomcat -keystore .tomcatKeyStore <br />
Enter keystore password: '''YOUR_KEYSTORE_PASSWORD'''<br />
Re-enter new password: '''YOUR_KEYSTORE_PASSWORD'''<br />
What is your first and last name?<br />
[Unknown]: '''localhost (e.g. your.domain.com)'''<br />
What is the name of your organizational unit?<br />
[Unknown]: '''Blahblah Development Team'''<br />
What is the name of your organization?<br />
[Unknown]: '''Your Company Name'''<br />
What is the name of your City or Locality?<br />
[Unknown]: '''Sydney'''<br />
What is the name of your State or Province?<br />
[Unknown]: '''New South Wales'''<br />
What is the two-letter country code for this unit?<br />
[Unknown]: '''AU'''<br />
Is CN=localhost, OU=BlahBlah Development Team, O=Your Company Name, L=Sydney, ST=New South Wales, C=AU correct?<br />
[no]: '''yes'''<br />
<br />
Enter key password for &lt;tomcat&gt;<br />
(RETURN if same as keystore password): '''PRESS_ENTER'''<br />
----<br />
<br />
=== Tomcat Configuration ===<br />
Go to the directory where the Tomcat is located.<br />
<br />
Open the <code>server.xml</code> file to edit.<br />
<source lang="bash"><br />
$TOMCAT_HOME/conf/server.xml<br />
</source><br />
<br />
Add the following lines<br />
<source lang="xml"><br />
<Connector SSLEnabled="true" acceptCount="100" clientAuth="false"<br />
disableUploadTimeout="true" enableLookups="true"<br />
keystoreFile="/path/to/keystore"<br />
keystorePass="YOUR_KEYSTORE_PASSWORD"<br />
maxSpareThreads="75" maxThreads="200" minSpareThreads="5" <br />
port="8443" scheme="https" secure="true" sslProtocol="TLS"/><br />
</source><br />
After<br />
<source lang="xml"><br />
<!-- A "Connector" represents an endpoint by which requests are received<br />
and responses are returned. Documentation at :<br />
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)<br />
Java AJP Connector: /docs/config/ajp.html<br />
APR (HTTP/AJP) Connector: /docs/apr.html<br />
Define a non-SSL HTTP/1.1 Connector on port 8080<br />
--><br />
<Connector port="8080" protocol="HTTP/1.1" <br />
connectionTimeout="20000" <br />
redirectPort="8443" /><br />
</source><br />
<br />
So it may look like<br />
<source lang="xml"><br />
<!-- A "Connector" represents an endpoint by which requests are received<br />
and responses are returned. Documentation at :<br />
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)<br />
Java AJP Connector: /docs/config/ajp.html<br />
APR (HTTP/AJP) Connector: /docs/apr.html<br />
Define a non-SSL HTTP/1.1 Connector on port 8080<br />
--><br />
<Connector port="8080" protocol="HTTP/1.1" <br />
connectionTimeout="20000" <br />
redirectPort="8443" /><br />
<br />
<!-- Added for SSL --><br />
<Connector SSLEnabled="true" acceptCount="100" clientAuth="false"<br />
disableUploadTimeout="true" enableLookups="true"<br />
keystoreFile="${user.home}/.tomcat_ssl/.tomcatKeyStore"<br />
keystorePass="YOUR_KEYSTORE_PASSWORD"<br />
maxSpareThreads="75" maxThreads="200" minSpareThreads="5" <br />
port="8443" scheme="https" secure="true" sslProtocol="TLS"/><br />
<br />
</source><br />
<br />
Open server.xml in <code>Eclipse workspace</code> to see if it is set correctly.<br />
<source lang="text"><br />
Server<br />
+Tomcat v6.0 Server at localhost-config<br />
+server.xml<br />
</source><br />
If the Eclipse workspace server.xml does not have it, added it. It is automatically added if the server is added to Eclipse workspace after server.xml modification happens (I am not sure if the same happens when the sever was added before modifying server.xml in the original tomcat).<br />
<br />
== Using Tomcat without Apache HTTP Server ==<br />
* Use [[Uncomplicated Firewall]]<br />
Open /etc/ufw/before.rules file and add the following lines to the top (after the first comment)<br />
# added by Kevin for Tomcat<br />
*nat<br />
:PREROUTING ACCEPT [0:0]<br />
-A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080<br />
:PREROUTING ACCEPT [0:0]<br />
-A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443<br />
COMMIT</div>Kevinhttp://kevinlee.io/wiki/Tar_ArchiveTar Archive2014-02-08T03:47:21Z<p>Kevin: /* Create and Split Tar File into Several Files */</p>
<hr />
<div>[[Category:Linux]]<br />
== Tar ==<br />
Tar is both a file format and the programme to handle that file.<br />
<br />
=== Create GZipped Tar File ===<br />
<pre><br />
$ tar -czvf tar_file_name.tar.gz input_file_or_directory_1 input_file_or_directory_2 ...<br />
</pre><br />
<br />
=== Extract GZipped Tar File ===<br />
<pre><br />
$ tar -xzvf tar_file_name.tar.gz<br />
</pre><br />
<br />
To extract file to a specific directory, use this.<br />
<pre><br />
$ tar -xzvf tar_file_name.tar.gz -C /directory/destination_directory<br />
</pre><br />
<br />
=== Create and Split Tar File into Several Files ===<br />
* create archives<br />
<pre><br />
$ tar cz large_file_1 large_file_2 | split -b 1024MiB - result_files.tgz_ <br />
</pre><br />
* uncompress<br />
<pre><br />
$ cat result_files.tgz_* | tar xz <br />
</pre><br />
<br />
* This solution avoids the need to use an intermediate large file when (de)compressing.<br />
* Use the tar -C option to specify a different directory for the resulting files.<br />
<br />
----<br />
<br />
<br />
* If only one file needs to be split, tar with a single source file is still fine but in this case, gzip can be used as well.<br />
* create archives<br />
<pre><br />
$ gzip -c one_large_file | split -b 1024MiB - result_files.gz_ <br />
</pre><br />
* uncompress<br />
<pre><br />
$ cat result_files.gz_* | gunzip -c > one_large_file <br />
</pre><br />
<br />
=== List Files Inside Tar File ===<br />
<pre><br />
$ tar -tzf tar_file_name.tar.gz<br />
</pre><br />
<br />
=== Create GZipped Tar with NOT GNU Tar ===<br />
<pre><br />
$ tar -cvf - input_file_or_directory_1 input_file_or_directory_2 | gzip > tar_file_name.tar.gz<br />
</pre><br />
<br />
-If <tt>'''gzip'''</tt> is not available, use <tt>'''compress'''</tt> instead.<br />
<br />
=== Extract GZipped Tar with NOT GNU Tar ===<br />
<pre><br />
gunzip -c tar_file_name.tar.gz | tar -xvf -<br />
</pre><br />
-If <tt>'''gunzip'''</tt> is not available, user <tt>'''uncompress'''</tt> instead.</div>Kevinhttp://kevinlee.io/wiki/Tar_ArchiveTar Archive2014-02-08T03:41:16Z<p>Kevin: /* Create and Split Tar File into Several Files */</p>
<hr />
<div>[[Category:Linux]]<br />
== Tar ==<br />
Tar is both a file format and the programme to handle that file.<br />
<br />
=== Create GZipped Tar File ===<br />
<pre><br />
$ tar -czvf tar_file_name.tar.gz input_file_or_directory_1 input_file_or_directory_2 ...<br />
</pre><br />
<br />
=== Extract GZipped Tar File ===<br />
<pre><br />
$ tar -xzvf tar_file_name.tar.gz<br />
</pre><br />
<br />
To extract file to a specific directory, use this.<br />
<pre><br />
$ tar -xzvf tar_file_name.tar.gz -C /directory/destination_directory<br />
</pre><br />
<br />
=== Create and Split Tar File into Several Files ===<br />
* create archives<br />
<pre><br />
$ tar cz large_file_1 large_file_2 | split -b 1024MiB - result_files.tgz_ <br />
</pre><br />
* uncompress<br />
<pre><br />
$ cat result_files.tgz_* | tar xz <br />
</pre><br />
<br />
* This solution avoids the need to use an intermediate large file when (de)compressing.<br />
* Use the tar -C option to use a different directory for the resulting files.<br />
<br />
----<br />
<br />
<br />
* If only one file needs to be split, tar with a single source file is still fine but in this case, gzip can be used as well.<br />
* create archives<br />
<pre><br />
$ gzip -c one_large_file | split -b 1024MiB - result_files.gz_<br />
</pre><br />
* uncompress<br />
<pre><br />
$ cat result_files.gz_* | gunzip -c > one_large_file<br />
</pre><br />
<br />
=== List Files Inside Tar File ===<br />
<pre><br />
$ tar -tzf tar_file_name.tar.gz<br />
</pre><br />
<br />
=== Create GZipped Tar with NOT GNU Tar ===<br />
<pre><br />
$ tar -cvf - input_file_or_directory_1 input_file_or_directory_2 | gzip > tar_file_name.tar.gz<br />
</pre><br />
<br />
-If <tt>'''gzip'''</tt> is not available, use <tt>'''compress'''</tt> instead.<br />
<br />
=== Extract GZipped Tar with NOT GNU Tar ===<br />
<pre><br />
gunzip -c tar_file_name.tar.gz | tar -xvf -<br />
</pre><br />
-If <tt>'''gunzip'''</tt> is not available, user <tt>'''uncompress'''</tt> instead.</div>Kevinhttp://kevinlee.io/wiki/Tar_ArchiveTar Archive2014-02-08T03:21:34Z<p>Kevin: </p>
<hr />
<div>[[Category:Linux]]<br />
== Tar ==<br />
Tar is both a file format and the programme to handle that file.<br />
<br />
=== Create GZipped Tar File ===<br />
<pre><br />
$ tar -czvf tar_file_name.tar.gz input_file_or_directory_1 input_file_or_directory_2 ...<br />
</pre><br />
<br />
=== Extract GZipped Tar File ===<br />
<pre><br />
$ tar -xzvf tar_file_name.tar.gz<br />
</pre><br />
<br />
To extract file to a specific directory, use this.<br />
<pre><br />
$ tar -xzvf tar_file_name.tar.gz -C /directory/destination_directory<br />
</pre><br />
<br />
=== Create and Split Tar File into Several Files ===<br />
* create archives<br />
<pre><br />
$ tar cz large_file_1 large_file_2 | split -b 1024MiB - result_files.tgz_ <br />
</pre><br />
* uncompress<br />
<pre><br />
$ cat result_files.tgz_* | tar xz <br />
</pre><br />
<br />
* This solution avoids the need to use an intermediate large file when (de)compressing.<br />
* Use the tar -C option to use a different directory for the resulting files.<br />
<br />
----<br />
<br />
<br />
* If only one file needs to be splited, tar with a single source file is still fine but in this case, gzip can be used as well.<br />
* create archives<br />
<pre><br />
$ gzip -c one_large_file | split -b 1024MiB - result_files.gz_<br />
</pre><br />
* uncompress<br />
<pre><br />
$ cat result_files.gz_* | gunzip -c > one_large_file<br />
</pre><br />
<br />
<br />
=== List Files Inside Tar File ===<br />
<pre><br />
$ tar -tzf tar_file_name.tar.gz<br />
</pre><br />
<br />
=== Create GZipped Tar with NOT GNU Tar ===<br />
<pre><br />
$ tar -cvf - input_file_or_directory_1 input_file_or_directory_2 | gzip > tar_file_name.tar.gz<br />
</pre><br />
<br />
-If <tt>'''gzip'''</tt> is not available, use <tt>'''compress'''</tt> instead.<br />
<br />
=== Extract GZipped Tar with NOT GNU Tar ===<br />
<pre><br />
gunzip -c tar_file_name.tar.gz | tar -xvf -<br />
</pre><br />
-If <tt>'''gunzip'''</tt> is not available, user <tt>'''uncompress'''</tt> instead.</div>Kevinhttp://kevinlee.io/wiki/Node.jsNode.js2013-11-17T11:44:22Z<p>Kevin: /* Install Node.js and NPM */</p>
<hr />
<div>[[Category:JavaScript]]<br />
= Install Node.js and NPM =<br />
<br />
$ git clone https://github.com/joyent/node.git <br />
$ cd node <br />
# Visit the [http://nodejs.org Node.js website] to find out the current version.<br />
$ git checkout v0.10.22 <br />
$ ./configure && make && sudo make install<br />
<br />
During installation, if the following error occurs (e.g. <code>g++: Command not found</code>), install the <code>build-essential</code> package.<br />
$ sudo apt-get install build-essential</div>Kevinhttp://kevinlee.io/wiki/Node.jsNode.js2013-11-17T11:44:01Z<p>Kevin: /* Install Node.js and NPM */</p>
<hr />
<div>[[Category:JavaScript]]<br />
= Install Node.js and NPM =<br />
<br />
$ git clone https://github.com/joyent/node.git <br />
$ cd node <br />
# Visit the [http://nodejs.org Node.js website] to find out the current version.<br />
$ git checkout v0.10.22 <br />
$ ./configure && make && sudo make install<br />
<br />
During installation, if the following error occurs (e.g. <code>g++: Command not found</code>, install the <code>build-essential</code> package.<br />
$ sudo apt-get install build-essential</div>Kevinhttp://kevinlee.io/wiki/Node.jsNode.js2013-11-17T11:33:03Z<p>Kevin: </p>
<hr />
<div>[[Category:JavaScript]]<br />
= Install Node.js and NPM =<br />
<br />
$ git clone https://github.com/joyent/node.git <br />
$ cd node <br />
# Visit the [http://nodejs.org Node.js website] to find out the current version.<br />
$ git checkout v0.10.22 <br />
$ ./configure && make && sudo make install<br />
<br />
During installation, if the following error occurs, install the <code>build-essential</code> package.<br />
$ sudo apt-get install build-essential</div>Kevinhttp://kevinlee.io/wiki/RubyRuby2013-11-17T11:30:27Z<p>Kevin: /* Header Installation */</p>
<hr />
<div>[[Category:Ruby]]<br />
<br />
= Installation =<br />
== Ruby Installation ==<br />
=== Install ruby 1.9.3 ===<br />
* Ubuntu 12.04 (it says 1.9.1 but is actually 1.9.3)<br />
<pre><br />
$ sudo apt-get install ruby1.9.1 <br />
</pre><br />
* Higher than 12.04 (not sure about 12.10 but at least 13.04 or higher)<br />
<pre><br />
$ sudo apt-get install ruby <br />
</pre><br />
<br />
== Header Installation ==<br />
* To install header files for compiling extension modules for Ruby,<br />
<pre><br />
# 12.04<br />
$ sudo apt-get install ruby1.9.1-dev<br />
</pre><br />
<br />
<pre><br />
# higher than 12.04<br />
$ sudo apt-get install ruby-dev <br />
</pre><br />
<br />
== Bundler Installation ==<br />
<pre><br />
$ sudo gem install bundler <br />
</pre></div>Kevinhttp://kevinlee.io/wiki/RubyRuby2013-11-17T11:28:44Z<p>Kevin: /* Ruby Installation */</p>
<hr />
<div>[[Category:Ruby]]<br />
<br />
= Installation =<br />
== Ruby Installation ==<br />
=== Install ruby 1.9.3 ===<br />
* Ubuntu 12.04 (it says 1.9.1 but is actually 1.9.3)<br />
<pre><br />
$ sudo apt-get install ruby1.9.1 <br />
</pre><br />
* Higher than 12.04 (not sure about 12.10 but at least 13.04 or higher)<br />
<pre><br />
$ sudo apt-get install ruby <br />
</pre><br />
<br />
== Header Installation ==<br />
* To install header files for compiling extension modules for Ruby,<br />
<pre><br />
$ sudo apt-get install ruby1.9.1-dev<br />
</pre><br />
<br />
== Bundler Installation ==<br />
<pre><br />
$ sudo gem install bundler <br />
</pre></div>Kevinhttp://kevinlee.io/wiki/Linux_Trouble_ShootingLinux Trouble Shooting2013-10-17T15:04:55Z<p>Kevin: Created page with "Category:Linux === Issue with Intel Graphics === * If there are any issues with Intel graphics, do <pre> $ sudo adduser $USER video </pre> * Then edit /etc/X11/xorg.conf (..."</p>
<hr />
<div>[[Category:Linux]]<br />
<br />
=== Issue with Intel Graphics ===<br />
* If there are any issues with Intel graphics, do<br />
<pre><br />
$ sudo adduser $USER video <br />
</pre><br />
<br />
* Then edit /etc/X11/xorg.conf (create if it does not exists)<br />
<br />
/etc/X11/xorg.conf<br />
---------------------------<br />
Section "Device"<br />
<br />
Identifier "Card0"<br />
<br />
Driver "intel"<br />
<br />
Option "AccelMethod" "uxa"<br />
<br />
#Option "AccelMethod" "sna"<br />
<br />
EndSection<br />
<br />
---------------------------</div>Kevinhttp://kevinlee.io/wiki/Main_MenuMain Menu2013-10-17T15:04:52Z<p>Kevin: </p>
<hr />
<div>[[Category:Linux]]<br />
<br />
==Trouble Shooting==<br />
When 'Main Menu' application breaks the main menu,<br />
remove <code>~/.config/menus/applications.menu</code file<br />
<pre><br />
$ rm ~/.config/menus/applications.menu<br />
</pre><br />
<br />
When the programmes installed through wine are not removed in the menu,<br />
check <code>~/.local/share/</code> directory and remove if there are any files and directories related to wine.</div>Kevinhttp://kevinlee.io/wiki/Main_MenuMain Menu2013-10-17T14:36:16Z<p>Kevin: </p>
<hr />
<div>[[Category:Linux]]<br />
<br />
==Trouble Shooting==<br />
=== Main Menu ===<br />
When 'Main Menu' application breaks the main menu,<br />
remove <code>~/.config/menus/applications.menu</code file<br />
<pre><br />
$ rm ~/.config/menus/applications.menu<br />
</pre><br />
<br />
When the programmes installed through wine are not removed in the menu,<br />
check <code>~/.local/share/</code> directory and remove if there are any files and directories related to wine.<br />
<br />
<br />
=== Issue with Intel Graphics ===<br />
* If there are any issues with Intel graphics, do<br />
<pre><br />
$ sudo adduser $USER video <br />
</pre><br />
<br />
* Then edit /etc/X11/xorg.conf (create if it does not exists)<br />
<br />
/etc/X11/xorg.conf<br />
---------------------------<br />
Section "Device"<br />
<br />
Identifier "Card0"<br />
<br />
Driver "intel"<br />
<br />
Option "AccelMethod" "uxa"<br />
<br />
#Option "AccelMethod" "sna"<br />
<br />
EndSection<br />
<br />
---------------------------</div>Kevinhttp://kevinlee.io/wiki/Node.jsNode.js2013-10-17T14:23:47Z<p>Kevin: /* Install Node.js and NPM */</p>
<hr />
<div>[[Category:JavaScript]]<br />
= Install Node.js and NPM =<br />
<br />
$ git clone https://github.com/joyent/node.git <br />
$ cd node <br />
# Visit the [http://nodejs.org Node.js website] to find out the current version.<br />
$ git checkout v0.10.20 <br />
$ ./configure && make && sudo make install<br />
<br />
During installation, if the following error occurs, install the <code>build-essential</code> package.<br />
$ sudo apt-get install build-essential</div>Kevinhttp://kevinlee.io/wiki/Node.jsNode.js2013-10-17T14:10:46Z<p>Kevin: /* Install Node.js and NPM */</p>
<hr />
<div>[[Category:JavaScript]]<br />
= Install Node.js and NPM =<br />
<br />
$ git clone https://github.com/joyent/node.git <br />
$ cd node <br />
# Visit the [http://nodejs.org Node.js website] to find out the current version.<br />
$ git checkout v0.10.20 <br />
$ ./configure && make && sudo make install</div>Kevinhttp://kevinlee.io/wiki/GitGit2013-10-02T08:55:31Z<p>Kevin: /* Commit */</p>
<hr />
<div>[[Category:Revision Control]]<br />
= Git =<br />
== Client-side Installation ==<br />
<br />
In the client,<br />
$ apt-get install git-core <br />
<br />
== Configuration ==<br />
=== User Info ===<br />
$ git config --global user.name "GivenName Surname" <br />
$ git config --global user.email user@email.address <br />
<br />
=== Default Text Editor ===<br />
$ git config --global core.editor "sublime-text -w"<br />
<br />
=== External Diff Tool ===<br />
==== Meld As External Diff Tool ====<br />
* To use Meld as a diff tool, install Meld<br />
$ sudo apt-get install meld <br />
* Set it as the default diff tool<br />
$ git config --global diff.external meld <br />
<br />
* It doesn't work and gives the following error messages.<br />
<pre><br />
$ git diff <br />
Usage: <br />
meld Start with an empty window<br />
meld <file|dir> Start a version control comparison<br />
meld <file> <file> [<file>] Start a 2- or 3-way file comparison<br />
meld <dir> <dir> [<dir>] Start a 2- or 3-way directory comparison<br />
meld <file> <dir> Start a comparison between file and dir/file<br />
<br />
meld: error: too many arguments (wanted 0-4, got 7)<br />
</pre><br />
<br />
* Create ~/diff.py<br />
<pre><br />
#!/usr/bin/python<br />
<br />
import sys<br />
import os<br />
<br />
os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))<br />
</pre><br />
* Make it executable<br />
$ chmod a+x ~/diff.py <br />
* Set the script as the diff tool<br />
$ git config --global diff.external ~/diff.py <br />
* Now it works<br />
$ git diff <br />
$ git diff HEAD <br />
<br />
<br />
==== Meld as Diff Tool ====<br />
* This way is much easier.<br />
$ git config --global diff.tool meld <br />
<br />
$ git difftool<br />
<br />
==== Meld as Diff Tool ====<br />
$ git config --global diff.tool vimdiff <br />
<br />
<br />
=== Unset External Diff Tool ===<br />
$ git config --global --unset diff.external <br />
<br />
<br />
=== Commit Template ===<br />
Create a template file<br/><br />
e.g.) $HOME/git/.your-config/git-commit-comment-template.txt<br />
CLOSED - #TICKET_NUMBER: Summary<br />
TICKET_URI<br />
Details<br />
<br />
$ git config --global commit.template $HOME/git/.your-config/git-commit-comment-template.txt <br />
<br />
<br />
=== Etc. ===<br />
$ git config --global color.ui true<br />
<br />
== Server-side Installation ==<br />
In the server,<br />
$ apt-get install git <br />
<br />
= Gitolite =<br />
== Add User to Manage Gitolite ==<br />
# Add user to manage gitolite <br />
<br />
$ adduser \<br />
--system \<br />
--shell /bin/bash \<br />
--gecos 'git version control' \<br />
--group \<br />
--disabled-password \<br />
--home /home/git \<br />
git<br />
<br />
Adding system user `git' (UID 108) ...<br />
Adding new group `git' (GID 110) ...<br />
Adding new user `git' (UID 108) with group `git' ...<br />
Creating home directory `/home/git' ...<br />
<br />
== Create SSH key ==<br />
$ ssh-keygen -t rsa <br />
/home/username/.ssh/some_git_rsa<br />
[ENTER]<br />
[ENTER]<br />
<br />
== Transfer SSH Keys to Server ==<br />
$ ssh-copy-id '-p PORT_NUMBER -i /home/user/.ssh/some_git_rsa.pub git_user@host' <br />
If it doesn't work because the user is the only user who can access the server with his/her ssh key so accessing with git_user doesn't work.<br />
<br />
$ scp -P PORT ~/.ssh/some_git_rsa.pub user@host: <br />
<br />
Log in with the user account<br />
$ ssh -p PORT user@host<br />
$ mv some_git_rsa.pub git.pub <br />
<br />
== Setup ==<br />
$ su - git <br />
$ cd ~ <br />
$ echo "PATH=$HOME/bin:$PATH" >> ~/.bashrc <br />
<br />
* don't need these 2 lines<br />
$ mkdir .ssh <br />
$ cat /home/user/git.pub >> /home/git/.ssh/authorized_keys <br />
<br />
$ exit<br />
<br />
== Gitolite Installation ==<br />
$ sudo -i <br />
$ apt-get install gitolite<br />
[ENTER] -> :q to exit<br />
$ exit<br />
<br />
== Setup git User ==<br />
$ su - git<br />
$ gl-setup /home/user/git.pub <br />
<br />
$ vim /etc/ssh/sshd_config <br />
Add git to the allowed user<br />
<br />
AllowUsers user git<br />
<br />
Restart ssh<br />
$ service ssh restart <br />
<br />
== Test ==<br />
* Testing (from user local machine)<br />
$ ssh -p PORT_NUMBER git@host <br />
Having many keys?<br />
$ ssh -p PORT_NUMBER -i ~/.ssh/user@host1.pub git@host <br />
<br />
For debugging<br />
$ ssh -p PORT_NUMBER -vT git@host <br />
<br />
$ mkdir some-dir <br />
$ cd some-dir <br />
$ git clone git@host:gitolite-admin <br />
Or if you use a specific port,<br />
$ git clone ssh://git@host:PORT_NUMBER/gitolite-admin<br />
<br />
== Add Git User (through Gitolite) ==<br />
Adding User<br />
$ cd gitolite-admin/keydir <br />
$ mkdir user_name <br />
$ cp -p ~/.ssh/user@host1.pub .<br />
$ cp -p ~/.ssh/user@host2.pub .<br />
$ git add user@host1.pub <br />
$ git commit -a -m "users added: user@host1 and user@host2" <br />
$ git push<br />
<br />
If directly copied to <code>/home/git/.gitolite/keydir</code>, run<br />
$ gl-setup<br />
to apply the changes<br />
<br />
= Usage =<br />
== Initializing a Repository in an Existing Directory ==<br />
* To make a directory a git repository, go to the project directory and run the following command<br />
<pre><br />
$ git init <br />
</pre><br />
Then it will create a new directory named <code>.git</code> inside the project directory. The <code>.git</code> folder contains all necessary files for the git repository.<br />
<br />
== Host Info Config using SSH_Config ==<br />
Create <code>~/.ssh/config</code> file and edit.<br />
<br />
Host host.name<br />
Hostname host.name<br />
Port 1111<br />
<br />
Host another.host.name<br />
Hostname another.host.name<br />
Port 2222<br />
<br />
Host host1<br />
Hostname host.name<br />
Port 1111<br />
User git<br />
IdentityFile ~/.ssh/git_rsa<br />
<br />
Host user-host2<br />
Hostname host2.name<br />
Port 1234<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
Host user-host3<br />
Hostname host3.com<br />
Port 1122<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
'''IdentityFile is a private key'''<br />
<br />
To use this info,<br />
$ git clone git@host1:repo-name <br />
# it will use 'git' as a user and access the server 'host.name' using the port nubmer 1111.<br />
<br />
$ git clone git@host1:repo-name <br />
# it will use 'user' as a user and access the server 'host2.name' using the port nubmer 1234.<br />
<br />
== Push to Specific Host ==<br />
<br />
$ git push git@host1:repo-name master<br />
<br />
== Set up Project Specific User Info ==<br />
* Move to the project repository<br />
$ git config user.email user@email.address <br />
<br />
To see and edit all the config details<br />
$ git config -e<br />
<br />
== .gitignore ==<br />
To exclude files when committing, create .gitignore file and put in the project root (git project repo root).<br />
For maven project, the content should be<br />
target/<br />
<br />
To set up the global ignore file<br />
$ git config --global core.excludesfile $HOME/git/.your-config/.global-gitignore <br />
<br />
Other gitignore files,<br />
https://github.com/github/gitignore<br />
<br />
<br />
=== .gitignore for Eclipse and Maven (for Kevin) ===<br />
<pre><br />
target/<br />
<br />
*.pydevproject<br />
.project<br />
.classpath<br />
.settings/<br />
.metadata<br />
bin/<br />
tmp/<br />
*.tmp<br />
*.bak<br />
*.swp<br />
*~.nib<br />
local.properties<br />
.loadpath<br />
<br />
# External tool builders<br />
.externalToolBuilders/<br />
<br />
# Locally stored "Eclipse launch configurations"<br />
*.launch<br />
<br />
# CDT-specific<br />
.cproject<br />
<br />
# PDT-specific<br />
.buildpath<br />
</pre><br />
<br />
<br />
== Commit ==<br />
* Add a file<br />
$ git add fileName<br />
<br />
* Commit changes<br />
$ git commit -m "comment"<br />
<br />
* Add all the files and folders<br />
$ git add -A <br />
# It is equivalent to git add .; git add -u<br />
# git add . -> add all changed and new ones but not removed ones<br />
# git add -u -> changed and removed but not new ones.<br />
<br />
=== Amend Last Comment ===<br />
* Amend the last comment<br />
$ git commit --amend -m "New comment"<br />
<br />
== Push ==<br />
=== Add Remote ===<br />
* To view all remote<br />
<pre><br />
$ git remote -v <br />
</pre><br />
<br />
* Add a remote<br />
<pre><br />
$ git remote add origin git@git-repo:project <br />
</pre><br />
Then<br />
<pre><br />
$ git push -u origin master <br />
</pre><br />
Or<br />
<pre><br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
</pre><br />
=== Push to Remote ===<br />
* Push to the remote git repository<br />
$ git push<br />
<br />
* If it gives error messages like:<br />
No refs in common and none specified; doing nothing.<br />
Perhaps you should specify a branch such as 'master'.<br />
(It doesn't happen if you have already done <code>git push -u origin master </code>).<br />
* Do<br />
$ git push origin master<br />
<br />
== Revert ==<br />
=== Reset ===<br />
* Revert to last commit.<br />
$ git reset --hard HEAD <br />
Or to specific commit/tag/branch<br />
$ git reset --hard <tag/branch/commit id> <br />
<br />
* Check the id using git log<br />
$ git log <br />
<br />
* To push it<br />
$ git push <reponame> -f <br />
<br />
=== Check out ===<br />
* Or change the name of master branch then check out from the commit to be used and make it master<br />
<br />
# Rename the current master branch<br />
#: <pre>$ git branch -m another_name_for_master </pre> <br />
# Check out from another commit:<br />
#: <pre> $ git checkout a1b2c3a3blah </pre><br />
# Make it the new master branch:<br />
#: <pre> $ git checkout -b master </pre><br />
* If the old master is no longer needed and has to be removed, remove it by<br />
$ git branch -D another_name_for_master<br />
<br />
<br />
=== Restore Removed File ===<br />
'''This has to be tested first then should be rewritten'''.<br /><br />
Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.<br />
$ git rev-list -n 1 HEAD -- <file_path> <br />
<br />
Then checkout the version at the commit before.<br />
$ git checkout <deleting_commit>^ -- <file_path> <br />
<br />
Or in one command, if $file is the file.<br />
$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"<br />
* Reference<br />
[http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top]<br />
<br />
<br />
== Branch ==<br />
* Create branch<br />
<pre><br />
$ git branch branch_name <br />
</pre><br />
* Change to the branch just created<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
OR<br />
* Create and change<br />
<pre><br />
git checkout -b branch_name <br />
</pre><br />
<br />
* Back to master<br />
<pre><br />
$ git checkout master <br />
</pre><br />
<br />
* To push,<br />
<pre><br />
$ git push origin branch_name <br />
</pre><br />
<br />
<br />
== Tag ==<br />
=== List Tags ===<br />
<pre><br />
$ git tag <br />
</pre><br />
<br />
=== View Tag ===<br />
* To view tag data with commit details.<br />
<pre><br />
$ git show tag_name <br />
</pre><br />
<br />
=== Create Tag ===<br />
* Create Tag<br />
<pre><br />
$ git tag -a tag_name -m 'tag message: new version blah blah' <br />
</pre><br />
* Tag later <br />
<pre><br />
$ git tag -a tag_name -m 'tag message' commit_checksum <br />
</pre><br />
e.g.)<br />
<pre><br />
$ git tag -a v1.5 -m 'new version with blah blah' b015e9d <br />
</pre><br />
* To see the list of checksum use the following command.<br />
<pre><br />
$ git log --pretty=oneline <br />
</pre><br />
<br />
=== Remove Tag ===<br />
<pre><br />
$ git tag -d tag_name <br />
</pre><br />
<br />
<br />
== Merge Multiple Git Repositories into One ==<br />
If the directory structures look like this<br />
<pre><br />
parent-dir (not git repo)<br />
│<br />
├─git-repo1 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
├─git-repo2 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
└─ git-repo3 ## each is connected to its own remote git repository<br />
├─src<br />
└.git<br />
</pre><br />
<br />
To make it like<br />
<pre><br />
parent-dir (now it is git repo)<br />
├─src ## contains all the files and sub-dirs from those three repositories (git-repo1, git-repo2 and git-repo3).<br />
└.git<br />
<br />
</pre><br />
and it still keeps all the histories from all those repositories, do following instructions.<br />
<br />
* Initialize the new repository directory.<br />
<pre><br />
$ cd parent-dir <br />
$ git init <br />
</pre><br />
<br />
* Pull the old ones and remove these one by one.<br />
<pre><br />
$ git pull git-repo1 <br />
<br />
## If there is any conflict, solve it first then<br />
## commit the changes<br />
$ git add file <br />
$ git commit -a <br />
## Now pull the one had conflict issue again.<br />
$ git pull git-repo1 <br />
<br />
$ rm -rf git-repo1 <br />
<br />
$ git pull git-repo2 <br />
$ rm -rf git-repo2 <br />
<br />
$ git pull git-repo3 <br />
$ rm -rf git-repo3 <br />
</pre><br />
<br />
* Done! Add the remote repo info if necessary.<br />
<br />
= Migration from SVN to Git =<br />
== Install git-svn ==<br />
$ apt-get install git-svn <br />
<br />
== Prepare for Migration ==<br />
In some dir, create users.txt having all the user info<br />
e.g.)<br />
USERNAME = FirstName LastName <user@host><br />
<br />
== Migrate ==<br />
=== Without Tags ===<br />
$ git svn clone --stdlayout --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project-tmp <br />
(https://svn.repo/some-parent-folder/project-folder <- Not trunk)<br />
<br />
$ cd project-tmp <br />
<br />
$ git svn fetch <br />
<br />
* To view the other SVN branches<br />
$ git branch -r<br />
<br />
$ cd ..<br />
<br />
$ git clone project-tmp project <br />
$ rm -Rf project-tmp <br />
<br />
$ cd project <br />
$ git remote rm origin <br />
<br />
* Before this, add git repo for the project first.<br />
$ git remote add origin git@git-repo:project <br />
<br />
* To view all remote<br />
$ git remote -v <br />
<br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
<br />
<br />
* add user info to project/.git/config<br />
[user]<br />
email = user@host<br />
<br />
$ git push -u origin master <br />
<br />
* Check status<br />
$ git status <br />
# On branch master<br />
nothing to commit (working directory clean)<br />
<br />
<br />
=== With Tags ===<br />
If there are nested tag folders like <code>tags/released/1.x/1.1.0, tags/released/1.x/1.2.0, tags/released/1.x/1.3.0, tags/released/2.x/2.0.1 and tags/released/2.x/2.2.0</code>, add <code>--tags</code> (for branches, it's <code>--branches</code>)<br />
$ git svn clone --stdlayout --tags=tags/*/*/* --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project <br />
$ cd project<br />
$ git svn fetch <br />
<br />
<pre><br />
#### not necessary as it will be handled differently (by create tag and remove the svn tag) <br />
$ cp -Rf .git/refs/remotes/tags/* .git/refs/tags/ <br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ cp -Rf .git/refs/remotes/* .git/refs/heads/ <br />
$ rm -Rf .git/refs/remotes <br />
###################################################################################<br />
</pre><br />
<br />
$ git branch -r<br />
Then for each tag listed do:<br />
<br />
$ git tag tagname tags/tagname<br />
$ git branch -r -d tags/tagname<br />
<br />
$ git tag tag-name-for-git tags/tag-name-from-svn<br />
$ git branch -r -d tags/tag-name-from-svn<br />
<br />
e.g.)<br />
$ git tag 0.1.15 tags/released/0.x/some-app-0.1.15<br />
$ git branch -r -d tags/released/0.x/some-app-0.1.15<br />
<br />
* list tags<br />
$ git tag -l<br />
<br />
* remove old svn tags<br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ git remote add origin git@git-repo:project <br />
<br />
* add user info to project/.git/config <br />
$ git config -e<br />
<br />
[user]<br />
email = user@host<br />
<br />
and remove [svn-remote "svn"] and [svn]<br />
$ git push origin --all <br />
<br />
It doesn't seem like the tags are pushed?<br />
$ git push origin --tags</div>Kevinhttp://kevinlee.io/wiki/Groovy-Eclipse_pluginGroovy-Eclipse plugin2013-10-02T08:42:38Z<p>Kevin: Redirected page to SpringSource Tool Suite#Install Groovy-Eclipse plugin</p>
<hr />
<div>#REDIRECT [[SpringSource Tool Suite#Install Groovy-Eclipse plugin]]<br />
[[Category:Groovy]]</div>Kevinhttp://kevinlee.io/wiki/Groovy-Eclipse_pluginGroovy-Eclipse plugin2013-10-02T08:41:33Z<p>Kevin: Redirected page to SpringSource Tool Suite</p>
<hr />
<div>#REDIRECT [[SpringSource Tool Suite]]<br />
[[Category:Groovy]]</div>Kevinhttp://kevinlee.io/wiki/Groovy-Eclipse_pluginGroovy-Eclipse plugin2013-10-02T08:40:19Z<p>Kevin: </p>
<hr />
<div>[[Category:Groovy]]<br />
#REDIRECT [[SpringSource Tool Suite]]</div>Kevinhttp://kevinlee.io/wiki/Groovy-Eclipse_pluginGroovy-Eclipse plugin2013-10-02T08:40:03Z<p>Kevin: </p>
<hr />
<div>[[Category:Groovy]]<br />
#REDIRECT [[SpringSource_Tool_Suite#Install_Groovy-Eclipse_plugin]]</div>Kevinhttp://kevinlee.io/wiki/Groovy-Eclipse_pluginGroovy-Eclipse plugin2013-10-02T08:38:20Z<p>Kevin: </p>
<hr />
<div>[[Category:Groovy]]<br />
#REDIRECT [[SpringSource Tool Suite#Install Groovy-Eclipse plugin]]</div>Kevinhttp://kevinlee.io/wiki/Groovy-Eclipse_pluginGroovy-Eclipse plugin2013-10-02T08:37:14Z<p>Kevin: Created page with "Category:Groovy #REDIRECT SpringSource Tool_Suite#Install Groovy-Eclipse plugin "</p>
<hr />
<div>[[Category:Groovy]]<br />
#REDIRECT [[SpringSource Tool_Suite#Install Groovy-Eclipse plugin ]]</div>Kevinhttp://kevinlee.io/wiki/SpringSource_Tool_SuiteSpringSource Tool Suite2013-10-02T08:35:40Z<p>Kevin: </p>
<hr />
<div>[[Category:Eclipse Plugins]]<br />
<br />
== Installation (SpringSource Tool Suite) ==<br />
=== Add Update Site ===<br />
* For Eclipse Juno (4.2) and Indigo (3.7)<br />
Read [http://www.springsource.org/STS-installation-instructions UPDATE SITE INSTALLATION INSTRUCTIONS]<br />
<br />
http://dist.springsource.com/release/TOOLS/update/e4.3 (for Eclipse Kepler)<br />
http://dist.springsource.com/release/TOOLS/update/e4.2 (for Eclipse Juno - using the new 4.2 stream)<br />
http://dist.springsource.com/release/TOOLS/update/e3.8 (for Eclipse Juno - using the 3.8 stream)<br />
http://dist.springsource.com/release/TOOLS/update/e3.7 (for Eclipse Indigo)<br />
<br />
* For Eclipse Helios (3.6)<br />
Read [http://dist.springsource.com/release/STS/doc/STS-installation_instructions.pdf STS Installation Instructions] and use the Eclipse update site for STS in the doc.<br />
Reference: http://blog.springsource.com/2009/06/24/installing-sts-into-eclipse-35/<br />
<br />
* Choose one!!! (The current repositories)<br />
Releases: http://www.springsource.com/update/e3.5<br />
Milestone releases: http://www.springsource.com/milestone/e3.5<br />
Nightly snapshots: http://www.springsource.com/snapshot/e3.5<br />
<br />
* Choose one!!! (Old ones)<br />
Releases: http://www.springsource.com/update/e3.4<br />
Milestone releases: http://www.springsource.com/milestone/e3.4<br />
Nightly snapshots: http://www.springsource.com/snapshot/e3.4<br />
<br />
=== Install ===<br />
* Select the components to install from the SpringSource Update Site for Eclipse 3.4 and 3.5 entry<br />
* Click the Install button.<br />
<br />
==== Install Groovy-Eclipse plugin ====<br />
<br />
-v Core / Eclipse Integration for Gradle<br />
└v Gradle IDE<br />
<br />
-v Groovy-Eclipse<br />
├v Groovy Compiler 1.8 Feature<br />
├v Groovy Compiler 2.1 Feature<br />
├v Groovy-Eclipse Feature<br />
└v JDT Core patch for Groovy-Eclipse plugin<br />
<br />
* Groovy-Eclipse: Select the version you have.<br />
<br />
== Installation (Old version - Spring IDE) ==<br />
=== Add Update Site ===<br />
* Add the following SpringSource Update site in the Update Manager.<br />
Releases: http://www.springsource.com/update/e3.4<br />
<br />
* It will add <code>SpringSource Update Site for Eclipse 3.4</code> and <code>AJDT Update Site</code>. If AJDT Update site is not added, add it manually ([[AspectJ Development Tools]]) and install it first.<br />
<br />
=== Install ===<br />
* Select '''<code>AspectJ Development Tools (Required)</code>''' in the <code>AJDT Update Site</code> entry (The other ones are optional).<br />
* Select the following components.<br />
-∨'''Core'''<br />
├∨'''Spring IDE Core (required)'''<br />
└∨'''SpringSource dm Server Tools (required)'''<br />
<br />
-∨'''Extensions'''<br />
├∨'''Spring IDE AOP Extension (optional)'''<br />
├∨'''Spring IDE Batch Extension (optional)'''<br />
├∨'''Spring IDE OSGi Extension (optional)'''<br />
├∨'''Spring IDE Security Extension (optional)'''<br />
└∨'''Spring IDE Web Flow Extension (optional)'''<br />
<br />
-∨'''Integrations'''<br />
├∨'''Spring IDE AJDT Integration (optional)<br />
└∨'''Spring IDE Mylyn Integration (optional)<br />
<br />
* Press the <code>Install</code> button.</div>Kevinhttp://kevinlee.io/wiki/Node.jsNode.js2013-09-21T18:44:38Z<p>Kevin: </p>
<hr />
<div>[[Category:JavaScript]]<br />
= Install Node.js and NPM =<br />
<br />
$ git clone https://github.com/joyent/node.git <br />
$ cd node <br />
# Visit the [http://nodejs.org Node.js website] to find out the current version.<br />
$ git checkout v0.10.18 <br />
$ ./configure && make && sudo make install</div>Kevinhttp://kevinlee.io/wiki/Node.jsNode.js2013-09-21T18:35:11Z<p>Kevin: </p>
<hr />
<div>[[Category:JavaScript]]<br />
= Install Node.js and NPM =<br />
<pre><br />
$ git clone https://github.com/joyent/node.git <br />
$ cd node <br />
#Try checking nodejs.org for what the stable version is<br />
$ git checkout v0.10.18 <br />
$ ./configure && make && sudo make install <br />
</pre></div>Kevinhttp://kevinlee.io/wiki/GitGit2013-09-13T14:18:55Z<p>Kevin: /* Usage */</p>
<hr />
<div>[[Category:Revision Control]]<br />
= Git =<br />
== Client-side Installation ==<br />
<br />
In the client,<br />
$ apt-get install git-core <br />
<br />
== Configuration ==<br />
=== User Info ===<br />
$ git config --global user.name "GivenName Surname" <br />
$ git config --global user.email user@email.address <br />
<br />
=== Default Text Editor ===<br />
$ git config --global core.editor "sublime-text -w"<br />
<br />
=== External Diff Tool ===<br />
==== Meld As External Diff Tool ====<br />
* To use Meld as a diff tool, install Meld<br />
$ sudo apt-get install meld <br />
* Set it as the default diff tool<br />
$ git config --global diff.external meld <br />
<br />
* It doesn't work and gives the following error messages.<br />
<pre><br />
$ git diff <br />
Usage: <br />
meld Start with an empty window<br />
meld <file|dir> Start a version control comparison<br />
meld <file> <file> [<file>] Start a 2- or 3-way file comparison<br />
meld <dir> <dir> [<dir>] Start a 2- or 3-way directory comparison<br />
meld <file> <dir> Start a comparison between file and dir/file<br />
<br />
meld: error: too many arguments (wanted 0-4, got 7)<br />
</pre><br />
<br />
* Create ~/diff.py<br />
<pre><br />
#!/usr/bin/python<br />
<br />
import sys<br />
import os<br />
<br />
os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))<br />
</pre><br />
* Make it executable<br />
$ chmod a+x ~/diff.py <br />
* Set the script as the diff tool<br />
$ git config --global diff.external ~/diff.py <br />
* Now it works<br />
$ git diff <br />
$ git diff HEAD <br />
<br />
<br />
==== Meld as Diff Tool ====<br />
* This way is much easier.<br />
$ git config --global diff.tool meld <br />
<br />
$ git difftool<br />
<br />
==== Meld as Diff Tool ====<br />
$ git config --global diff.tool vimdiff <br />
<br />
<br />
=== Unset External Diff Tool ===<br />
$ git config --global --unset diff.external <br />
<br />
<br />
=== Commit Template ===<br />
Create a template file<br/><br />
e.g.) $HOME/git/.your-config/git-commit-comment-template.txt<br />
CLOSED - #TICKET_NUMBER: Summary<br />
TICKET_URI<br />
Details<br />
<br />
$ git config --global commit.template $HOME/git/.your-config/git-commit-comment-template.txt <br />
<br />
<br />
=== Etc. ===<br />
$ git config --global color.ui true<br />
<br />
== Server-side Installation ==<br />
In the server,<br />
$ apt-get install git <br />
<br />
= Gitolite =<br />
== Add User to Manage Gitolite ==<br />
# Add user to manage gitolite <br />
<br />
$ adduser \<br />
--system \<br />
--shell /bin/bash \<br />
--gecos 'git version control' \<br />
--group \<br />
--disabled-password \<br />
--home /home/git \<br />
git<br />
<br />
Adding system user `git' (UID 108) ...<br />
Adding new group `git' (GID 110) ...<br />
Adding new user `git' (UID 108) with group `git' ...<br />
Creating home directory `/home/git' ...<br />
<br />
== Create SSH key ==<br />
$ ssh-keygen -t rsa <br />
/home/username/.ssh/some_git_rsa<br />
[ENTER]<br />
[ENTER]<br />
<br />
== Transfer SSH Keys to Server ==<br />
$ ssh-copy-id '-p PORT_NUMBER -i /home/user/.ssh/some_git_rsa.pub git_user@host' <br />
If it doesn't work because the user is the only user who can access the server with his/her ssh key so accessing with git_user doesn't work.<br />
<br />
$ scp -P PORT ~/.ssh/some_git_rsa.pub user@host: <br />
<br />
Log in with the user account<br />
$ ssh -p PORT user@host<br />
$ mv some_git_rsa.pub git.pub <br />
<br />
== Setup ==<br />
$ su - git <br />
$ cd ~ <br />
$ echo "PATH=$HOME/bin:$PATH" >> ~/.bashrc <br />
<br />
* don't need these 2 lines<br />
$ mkdir .ssh <br />
$ cat /home/user/git.pub >> /home/git/.ssh/authorized_keys <br />
<br />
$ exit<br />
<br />
== Gitolite Installation ==<br />
$ sudo -i <br />
$ apt-get install gitolite<br />
[ENTER] -> :q to exit<br />
$ exit<br />
<br />
== Setup git User ==<br />
$ su - git<br />
$ gl-setup /home/user/git.pub <br />
<br />
$ vim /etc/ssh/sshd_config <br />
Add git to the allowed user<br />
<br />
AllowUsers user git<br />
<br />
Restart ssh<br />
$ service ssh restart <br />
<br />
== Test ==<br />
* Testing (from user local machine)<br />
$ ssh -p PORT_NUMBER git@host <br />
Having many keys?<br />
$ ssh -p PORT_NUMBER -i ~/.ssh/user@host1.pub git@host <br />
<br />
For debugging<br />
$ ssh -p PORT_NUMBER -vT git@host <br />
<br />
$ mkdir some-dir <br />
$ cd some-dir <br />
$ git clone git@host:gitolite-admin <br />
Or if you use a specific port,<br />
$ git clone ssh://git@host:PORT_NUMBER/gitolite-admin<br />
<br />
== Add Git User (through Gitolite) ==<br />
Adding User<br />
$ cd gitolite-admin/keydir <br />
$ mkdir user_name <br />
$ cp -p ~/.ssh/user@host1.pub .<br />
$ cp -p ~/.ssh/user@host2.pub .<br />
$ git add user@host1.pub <br />
$ git commit -a -m "users added: user@host1 and user@host2" <br />
$ git push<br />
<br />
If directly copied to <code>/home/git/.gitolite/keydir</code>, run<br />
$ gl-setup<br />
to apply the changes<br />
<br />
= Usage =<br />
== Initializing a Repository in an Existing Directory ==<br />
* To make a directory a git repository, go to the project directory and run the following command<br />
<pre><br />
$ git init <br />
</pre><br />
Then it will create a new directory named <code>.git</code> inside the project directory. The <code>.git</code> folder contains all necessary files for the git repository.<br />
<br />
== Host Info Config using SSH_Config ==<br />
Create <code>~/.ssh/config</code> file and edit.<br />
<br />
Host host.name<br />
Hostname host.name<br />
Port 1111<br />
<br />
Host another.host.name<br />
Hostname another.host.name<br />
Port 2222<br />
<br />
Host host1<br />
Hostname host.name<br />
Port 1111<br />
User git<br />
IdentityFile ~/.ssh/git_rsa<br />
<br />
Host user-host2<br />
Hostname host2.name<br />
Port 1234<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
Host user-host3<br />
Hostname host3.com<br />
Port 1122<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
'''IdentityFile is a private key'''<br />
<br />
To use this info,<br />
$ git clone git@host1:repo-name <br />
# it will use 'git' as a user and access the server 'host.name' using the port nubmer 1111.<br />
<br />
$ git clone git@host1:repo-name <br />
# it will use 'user' as a user and access the server 'host2.name' using the port nubmer 1234.<br />
<br />
== Push to Specific Host ==<br />
<br />
$ git push git@host1:repo-name master<br />
<br />
== Set up Project Specific User Info ==<br />
* Move to the project repository<br />
$ git config user.email user@email.address <br />
<br />
To see and edit all the config details<br />
$ git config -e<br />
<br />
== .gitignore ==<br />
To exclude files when committing, create .gitignore file and put in the project root (git project repo root).<br />
For maven project, the content should be<br />
target/<br />
<br />
To set up the global ignore file<br />
$ git config --global core.excludesfile $HOME/git/.your-config/.global-gitignore <br />
<br />
Other gitignore files,<br />
https://github.com/github/gitignore<br />
<br />
<br />
=== .gitignore for Eclipse and Maven (for Kevin) ===<br />
<pre><br />
target/<br />
<br />
*.pydevproject<br />
.project<br />
.classpath<br />
.settings/<br />
.metadata<br />
bin/<br />
tmp/<br />
*.tmp<br />
*.bak<br />
*.swp<br />
*~.nib<br />
local.properties<br />
.loadpath<br />
<br />
# External tool builders<br />
.externalToolBuilders/<br />
<br />
# Locally stored "Eclipse launch configurations"<br />
*.launch<br />
<br />
# CDT-specific<br />
.cproject<br />
<br />
# PDT-specific<br />
.buildpath<br />
</pre><br />
<br />
<br />
== Commit ==<br />
* Add file<br />
$ git add fileName<br />
<br />
* Commit<br />
$ git commit -m "comment"<br />
<br />
* Add all the files and folders<br />
$ git add -A <br />
# It is equivalent to git add .; git add -u<br />
# git add . -> add all changed and new ones but not removed ones<br />
# git add -u -> changed and removed but not new ones.<br />
<br />
=== Amend Last Comment ===<br />
* Amend the last comment<br />
$ git commit --amend -m "New comment"<br />
<br />
<br />
== Push ==<br />
=== Add Remote ===<br />
* To view all remote<br />
<pre><br />
$ git remote -v <br />
</pre><br />
<br />
* Add a remote<br />
<pre><br />
$ git remote add origin git@git-repo:project <br />
</pre><br />
Then<br />
<pre><br />
$ git push -u origin master <br />
</pre><br />
Or<br />
<pre><br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
</pre><br />
=== Push to Remote ===<br />
* Push to the remote git repository<br />
$ git push<br />
<br />
* If it gives error messages like:<br />
No refs in common and none specified; doing nothing.<br />
Perhaps you should specify a branch such as 'master'.<br />
(It doesn't happen if you have already done <code>git push -u origin master </code>).<br />
* Do<br />
$ git push origin master<br />
<br />
== Revert ==<br />
=== Reset ===<br />
* Revert to last commit.<br />
$ git reset --hard HEAD <br />
Or to specific commit/tag/branch<br />
$ git reset --hard <tag/branch/commit id> <br />
<br />
* Check the id using git log<br />
$ git log <br />
<br />
* To push it<br />
$ git push <reponame> -f <br />
<br />
=== Check out ===<br />
* Or change the name of master branch then check out from the commit to be used and make it master<br />
<br />
# Rename the current master branch<br />
#: <pre>$ git branch -m another_name_for_master </pre> <br />
# Check out from another commit:<br />
#: <pre> $ git checkout a1b2c3a3blah </pre><br />
# Make it the new master branch:<br />
#: <pre> $ git checkout -b master </pre><br />
* If the old master is no longer needed and has to be removed, remove it by<br />
$ git branch -D another_name_for_master<br />
<br />
<br />
=== Restore Removed File ===<br />
'''This has to be tested first then should be rewritten'''.<br /><br />
Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.<br />
$ git rev-list -n 1 HEAD -- <file_path> <br />
<br />
Then checkout the version at the commit before.<br />
$ git checkout <deleting_commit>^ -- <file_path> <br />
<br />
Or in one command, if $file is the file.<br />
$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"<br />
* Reference<br />
[http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top]<br />
<br />
<br />
== Branch ==<br />
* Create branch<br />
<pre><br />
$ git branch branch_name <br />
</pre><br />
* Change to the branch just created<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
OR<br />
* Create and change<br />
<pre><br />
git checkout -b branch_name <br />
</pre><br />
<br />
* Back to master<br />
<pre><br />
$ git checkout master <br />
</pre><br />
<br />
* To push,<br />
<pre><br />
$ git push origin branch_name <br />
</pre><br />
<br />
<br />
== Tag ==<br />
=== List Tags ===<br />
<pre><br />
$ git tag <br />
</pre><br />
<br />
=== View Tag ===<br />
* To view tag data with commit details.<br />
<pre><br />
$ git show tag_name <br />
</pre><br />
<br />
=== Create Tag ===<br />
* Create Tag<br />
<pre><br />
$ git tag -a tag_name -m 'tag message: new version blah blah' <br />
</pre><br />
* Tag later <br />
<pre><br />
$ git tag -a tag_name -m 'tag message' commit_checksum <br />
</pre><br />
e.g.)<br />
<pre><br />
$ git tag -a v1.5 -m 'new version with blah blah' b015e9d <br />
</pre><br />
* To see the list of checksum use the following command.<br />
<pre><br />
$ git log --pretty=oneline <br />
</pre><br />
<br />
=== Remove Tag ===<br />
<pre><br />
$ git tag -d tag_name <br />
</pre><br />
<br />
<br />
== Merge Multiple Git Repositories into One ==<br />
If the directory structures look like this<br />
<pre><br />
parent-dir (not git repo)<br />
│<br />
├─git-repo1 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
├─git-repo2 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
└─ git-repo3 ## each is connected to its own remote git repository<br />
├─src<br />
└.git<br />
</pre><br />
<br />
To make it like<br />
<pre><br />
parent-dir (now it is git repo)<br />
├─src ## contains all the files and sub-dirs from those three repositories (git-repo1, git-repo2 and git-repo3).<br />
└.git<br />
<br />
</pre><br />
and it still keeps all the histories from all those repositories, do following instructions.<br />
<br />
* Initialize the new repository directory.<br />
<pre><br />
$ cd parent-dir <br />
$ git init <br />
</pre><br />
<br />
* Pull the old ones and remove these one by one.<br />
<pre><br />
$ git pull git-repo1 <br />
<br />
## If there is any conflict, solve it first then<br />
## commit the changes<br />
$ git add file <br />
$ git commit -a <br />
## Now pull the one had conflict issue again.<br />
$ git pull git-repo1 <br />
<br />
$ rm -rf git-repo1 <br />
<br />
$ git pull git-repo2 <br />
$ rm -rf git-repo2 <br />
<br />
$ git pull git-repo3 <br />
$ rm -rf git-repo3 <br />
</pre><br />
<br />
* Done! Add the remote repo info if necessary.<br />
<br />
= Migration from SVN to Git =<br />
== Install git-svn ==<br />
$ apt-get install git-svn <br />
<br />
== Prepare for Migration ==<br />
In some dir, create users.txt having all the user info<br />
e.g.)<br />
USERNAME = FirstName LastName <user@host><br />
<br />
== Migrate ==<br />
=== Without Tags ===<br />
$ git svn clone --stdlayout --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project-tmp <br />
(https://svn.repo/some-parent-folder/project-folder <- Not trunk)<br />
<br />
$ cd project-tmp <br />
<br />
$ git svn fetch <br />
<br />
* To view the other SVN branches<br />
$ git branch -r<br />
<br />
$ cd ..<br />
<br />
$ git clone project-tmp project <br />
$ rm -Rf project-tmp <br />
<br />
$ cd project <br />
$ git remote rm origin <br />
<br />
* Before this, add git repo for the project first.<br />
$ git remote add origin git@git-repo:project <br />
<br />
* To view all remote<br />
$ git remote -v <br />
<br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
<br />
<br />
* add user info to project/.git/config<br />
[user]<br />
email = user@host<br />
<br />
$ git push -u origin master <br />
<br />
* Check status<br />
$ git status <br />
# On branch master<br />
nothing to commit (working directory clean)<br />
<br />
<br />
=== With Tags ===<br />
If there are nested tag folders like <code>tags/released/1.x/1.1.0, tags/released/1.x/1.2.0, tags/released/1.x/1.3.0, tags/released/2.x/2.0.1 and tags/released/2.x/2.2.0</code>, add <code>--tags</code> (for branches, it's <code>--branches</code>)<br />
$ git svn clone --stdlayout --tags=tags/*/*/* --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project <br />
$ cd project<br />
$ git svn fetch <br />
<br />
<pre><br />
#### not necessary as it will be handled differently (by create tag and remove the svn tag) <br />
$ cp -Rf .git/refs/remotes/tags/* .git/refs/tags/ <br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ cp -Rf .git/refs/remotes/* .git/refs/heads/ <br />
$ rm -Rf .git/refs/remotes <br />
###################################################################################<br />
</pre><br />
<br />
$ git branch -r<br />
Then for each tag listed do:<br />
<br />
$ git tag tagname tags/tagname<br />
$ git branch -r -d tags/tagname<br />
<br />
$ git tag tag-name-for-git tags/tag-name-from-svn<br />
$ git branch -r -d tags/tag-name-from-svn<br />
<br />
e.g.)<br />
$ git tag 0.1.15 tags/released/0.x/some-app-0.1.15<br />
$ git branch -r -d tags/released/0.x/some-app-0.1.15<br />
<br />
* list tags<br />
$ git tag -l<br />
<br />
* remove old svn tags<br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ git remote add origin git@git-repo:project <br />
<br />
* add user info to project/.git/config <br />
$ git config -e<br />
<br />
[user]<br />
email = user@host<br />
<br />
and remove [svn-remote "svn"] and [svn]<br />
$ git push origin --all <br />
<br />
It doesn't seem like the tags are pushed?<br />
$ git push origin --tags</div>Kevinhttp://kevinlee.io/wiki/GitGit2013-09-13T14:17:56Z<p>Kevin: /* Tag */</p>
<hr />
<div>[[Category:Revision Control]]<br />
= Git =<br />
== Client-side Installation ==<br />
<br />
In the client,<br />
$ apt-get install git-core <br />
<br />
== Configuration ==<br />
=== User Info ===<br />
$ git config --global user.name "GivenName Surname" <br />
$ git config --global user.email user@email.address <br />
<br />
=== Default Text Editor ===<br />
$ git config --global core.editor "sublime-text -w"<br />
<br />
=== External Diff Tool ===<br />
==== Meld As External Diff Tool ====<br />
* To use Meld as a diff tool, install Meld<br />
$ sudo apt-get install meld <br />
* Set it as the default diff tool<br />
$ git config --global diff.external meld <br />
<br />
* It doesn't work and gives the following error messages.<br />
<pre><br />
$ git diff <br />
Usage: <br />
meld Start with an empty window<br />
meld <file|dir> Start a version control comparison<br />
meld <file> <file> [<file>] Start a 2- or 3-way file comparison<br />
meld <dir> <dir> [<dir>] Start a 2- or 3-way directory comparison<br />
meld <file> <dir> Start a comparison between file and dir/file<br />
<br />
meld: error: too many arguments (wanted 0-4, got 7)<br />
</pre><br />
<br />
* Create ~/diff.py<br />
<pre><br />
#!/usr/bin/python<br />
<br />
import sys<br />
import os<br />
<br />
os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))<br />
</pre><br />
* Make it executable<br />
$ chmod a+x ~/diff.py <br />
* Set the script as the diff tool<br />
$ git config --global diff.external ~/diff.py <br />
* Now it works<br />
$ git diff <br />
$ git diff HEAD <br />
<br />
<br />
==== Meld as Diff Tool ====<br />
* This way is much easier.<br />
$ git config --global diff.tool meld <br />
<br />
$ git difftool<br />
<br />
==== Meld as Diff Tool ====<br />
$ git config --global diff.tool vimdiff <br />
<br />
<br />
=== Unset External Diff Tool ===<br />
$ git config --global --unset diff.external <br />
<br />
<br />
=== Commit Template ===<br />
Create a template file<br/><br />
e.g.) $HOME/git/.your-config/git-commit-comment-template.txt<br />
CLOSED - #TICKET_NUMBER: Summary<br />
TICKET_URI<br />
Details<br />
<br />
$ git config --global commit.template $HOME/git/.your-config/git-commit-comment-template.txt <br />
<br />
<br />
=== Etc. ===<br />
$ git config --global color.ui true<br />
<br />
== Server-side Installation ==<br />
In the server,<br />
$ apt-get install git <br />
<br />
= Gitolite =<br />
== Add User to Manage Gitolite ==<br />
# Add user to manage gitolite <br />
<br />
$ adduser \<br />
--system \<br />
--shell /bin/bash \<br />
--gecos 'git version control' \<br />
--group \<br />
--disabled-password \<br />
--home /home/git \<br />
git<br />
<br />
Adding system user `git' (UID 108) ...<br />
Adding new group `git' (GID 110) ...<br />
Adding new user `git' (UID 108) with group `git' ...<br />
Creating home directory `/home/git' ...<br />
<br />
== Create SSH key ==<br />
$ ssh-keygen -t rsa <br />
/home/username/.ssh/some_git_rsa<br />
[ENTER]<br />
[ENTER]<br />
<br />
== Transfer SSH Keys to Server ==<br />
$ ssh-copy-id '-p PORT_NUMBER -i /home/user/.ssh/some_git_rsa.pub git_user@host' <br />
If it doesn't work because the user is the only user who can access the server with his/her ssh key so accessing with git_user doesn't work.<br />
<br />
$ scp -P PORT ~/.ssh/some_git_rsa.pub user@host: <br />
<br />
Log in with the user account<br />
$ ssh -p PORT user@host<br />
$ mv some_git_rsa.pub git.pub <br />
<br />
== Setup ==<br />
$ su - git <br />
$ cd ~ <br />
$ echo "PATH=$HOME/bin:$PATH" >> ~/.bashrc <br />
<br />
* don't need these 2 lines<br />
$ mkdir .ssh <br />
$ cat /home/user/git.pub >> /home/git/.ssh/authorized_keys <br />
<br />
$ exit<br />
<br />
== Gitolite Installation ==<br />
$ sudo -i <br />
$ apt-get install gitolite<br />
[ENTER] -> :q to exit<br />
$ exit<br />
<br />
== Setup git User ==<br />
$ su - git<br />
$ gl-setup /home/user/git.pub <br />
<br />
$ vim /etc/ssh/sshd_config <br />
Add git to the allowed user<br />
<br />
AllowUsers user git<br />
<br />
Restart ssh<br />
$ service ssh restart <br />
<br />
== Test ==<br />
* Testing (from user local machine)<br />
$ ssh -p PORT_NUMBER git@host <br />
Having many keys?<br />
$ ssh -p PORT_NUMBER -i ~/.ssh/user@host1.pub git@host <br />
<br />
For debugging<br />
$ ssh -p PORT_NUMBER -vT git@host <br />
<br />
$ mkdir some-dir <br />
$ cd some-dir <br />
$ git clone git@host:gitolite-admin <br />
Or if you use a specific port,<br />
$ git clone ssh://git@host:PORT_NUMBER/gitolite-admin<br />
<br />
== Add Git User (through Gitolite) ==<br />
Adding User<br />
$ cd gitolite-admin/keydir <br />
$ mkdir user_name <br />
$ cp -p ~/.ssh/user@host1.pub .<br />
$ cp -p ~/.ssh/user@host2.pub .<br />
$ git add user@host1.pub <br />
$ git commit -a -m "users added: user@host1 and user@host2" <br />
$ git push<br />
<br />
If directly copied to <code>/home/git/.gitolite/keydir</code>, run<br />
$ gl-setup<br />
to apply the changes<br />
<br />
= Usage =<br />
== Initializing a Repository in an Existing Directory ==<br />
* To make a directory a git repository, go to the project directory and run the following command<br />
<pre><br />
$ git init <br />
</pre><br />
Then it will create a new directory named <code>.git</code> inside the project directory. The <code>.git</code> folder contains all necessary files for the git repository.<br />
<br />
== Host Info Config using SSH_Config ==<br />
Create <code>~/.ssh/config</code> file and edit.<br />
<br />
Host host.name<br />
Hostname host.name<br />
Port 1111<br />
<br />
Host another.host.name<br />
Hostname another.host.name<br />
Port 2222<br />
<br />
Host host1<br />
Hostname host.name<br />
Port 1111<br />
User git<br />
IdentityFile ~/.ssh/git_rsa<br />
<br />
Host user-host2<br />
Hostname host2.name<br />
Port 1234<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
Host user-host3<br />
Hostname host3.com<br />
Port 1122<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
'''IdentityFile is a private key'''<br />
<br />
To use this info,<br />
$ git clone git@host1:repo-name <br />
# it will use 'git' as a user and access the server 'host.name' using the port nubmer 1111.<br />
<br />
$ git clone git@host1:repo-name <br />
# it will use 'user' as a user and access the server 'host2.name' using the port nubmer 1234.<br />
<br />
== Push to Specific Host ==<br />
<br />
$ git push git@host1:repo-name master<br />
<br />
== Set up Project Specific User Info ==<br />
* Move to the project repository<br />
$ git config user.email user@email.address <br />
<br />
To see and edit all the config details<br />
$ git config -e<br />
<br />
== .gitignore ==<br />
To exclude files when committing, create .gitignore file and put in the project root (git project repo root).<br />
For maven project, the content should be<br />
target/<br />
<br />
To set up the global ignore file<br />
$ git config --global core.excludesfile $HOME/git/.your-config/.global-gitignore <br />
<br />
Other gitignore files,<br />
https://github.com/github/gitignore<br />
<br />
<br />
=== .gitignore for Eclipse and Maven (for Kevin) ===<br />
<pre><br />
target/<br />
<br />
*.pydevproject<br />
.project<br />
.classpath<br />
.settings/<br />
.metadata<br />
bin/<br />
tmp/<br />
*.tmp<br />
*.bak<br />
*.swp<br />
*~.nib<br />
local.properties<br />
.loadpath<br />
<br />
# External tool builders<br />
.externalToolBuilders/<br />
<br />
# Locally stored "Eclipse launch configurations"<br />
*.launch<br />
<br />
# CDT-specific<br />
.cproject<br />
<br />
# PDT-specific<br />
.buildpath<br />
</pre><br />
<br />
<br />
== Commit ==<br />
* Add file<br />
$ git add fileName<br />
<br />
* Commit<br />
$ git commit -m "comment"<br />
<br />
* Add all the files and folders<br />
$ git add -A <br />
# It is equivalent to git add .; git add -u<br />
# git add . -> add all changed and new ones but not removed ones<br />
# git add -u -> changed and removed but not new ones.<br />
<br />
=== Amend Last Comment ===<br />
* Amend the last comment<br />
$ git commit --amend -m "New comment"<br />
<br />
<br />
== Push ==<br />
=== Add Remote ===<br />
* To view all remote<br />
<pre><br />
$ git remote -v <br />
</pre><br />
<br />
* Add a remote<br />
<pre><br />
$ git remote add origin git@git-repo:project <br />
</pre><br />
Then<br />
<pre><br />
$ git push -u origin master <br />
</pre><br />
Or<br />
<pre><br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
</pre><br />
=== Push to Remote ===<br />
* Push to the remote git repository<br />
$ git push<br />
<br />
* If it gives error messages like:<br />
No refs in common and none specified; doing nothing.<br />
Perhaps you should specify a branch such as 'master'.<br />
(It doesn't happen if you have already done <code>git push -u origin master </code>).<br />
* Do<br />
$ git push origin master<br />
<br />
== Revert ==<br />
=== Reset ===<br />
* Revert to last commit.<br />
$ git reset --hard HEAD <br />
Or to specific commit/tag/branch<br />
$ git reset --hard <tag/branch/commit id> <br />
<br />
* Check the id using git log<br />
$ git log <br />
<br />
* To push it<br />
$ git push <reponame> -f <br />
<br />
=== Check out ===<br />
* Or change the name of master branch then check out from the commit to be used and make it master<br />
<br />
# Rename the current master branch<br />
#: <pre>$ git branch -m another_name_for_master </pre> <br />
# Check out from another commit:<br />
#: <pre> $ git checkout a1b2c3a3blah </pre><br />
# Make it the new master branch:<br />
#: <pre> $ git checkout -b master </pre><br />
* If the old master is no longer needed and has to be removed, remove it by<br />
$ git branch -D another_name_for_master<br />
<br />
<br />
=== Restore Removed File ===<br />
'''This has to be tested first then should be rewritten'''.<br /><br />
Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.<br />
$ git rev-list -n 1 HEAD -- <file_path> <br />
<br />
Then checkout the version at the commit before.<br />
$ git checkout <deleting_commit>^ -- <file_path> <br />
<br />
Or in one command, if $file is the file.<br />
$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"<br />
* Reference<br />
[http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top]<br />
<br />
<br />
== Branch ==<br />
* Create branch<br />
<pre><br />
$ git branch branch_name <br />
</pre><br />
* Change to the branch just created<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
OR<br />
* Create and change<br />
<pre><br />
git checkout -b branch_name <br />
</pre><br />
<br />
* Back to master<br />
<pre><br />
$ git checkout master <br />
</pre><br />
<br />
* To push,<br />
<pre><br />
$ git push origin branch_name <br />
</pre><br />
<br />
== Tag ==<br />
=== List Tags ===<br />
<pre><br />
$ git tag <br />
</pre><br />
<br />
<br />
=== View Tag ===<br />
* To view tag data with commit details.<br />
<pre><br />
$ git show tag_name <br />
</pre><br />
<br />
<br />
=== Create Tag ===<br />
* Create Tag<br />
<pre><br />
$ git tag -a tag_name -m 'tag message: new version blah blah' <br />
</pre><br />
* Tag later <br />
<pre><br />
$ git tag -a tag_name -m 'tag message' commit_checksum <br />
</pre><br />
e.g.)<br />
<pre><br />
$ git tag -a v1.5 -m 'new version with blah blah' b015e9d <br />
</pre><br />
* To see the list of checksum use the following command.<br />
<pre><br />
$ git log --pretty=oneline <br />
</pre><br />
<br />
<br />
=== Remove Tag ===<br />
<pre><br />
$ git tag -d tag_name <br />
</pre><br />
<br />
== Merge Multiple Git Repositories into One ==<br />
If the directory structures look like this<br />
<pre><br />
parent-dir (not git repo)<br />
│<br />
├─git-repo1 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
├─git-repo2 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
└─ git-repo3 ## each is connected to its own remote git repository<br />
├─src<br />
└.git<br />
</pre><br />
<br />
To make it like<br />
<pre><br />
parent-dir (now it is git repo)<br />
├─src ## contains all the files and sub-dirs from those three repositories (git-repo1, git-repo2 and git-repo3).<br />
└.git<br />
<br />
</pre><br />
and it still keeps all the histories from all those repositories, do following instructions.<br />
<br />
* Initialize the new repository directory.<br />
<pre><br />
$ cd parent-dir <br />
$ git init <br />
</pre><br />
<br />
* Pull the old ones and remove these one by one.<br />
<pre><br />
$ git pull git-repo1 <br />
<br />
## If there is any conflict, solve it first then<br />
## commit the changes<br />
$ git add file <br />
$ git commit -a <br />
## Now pull the one had conflict issue again.<br />
$ git pull git-repo1 <br />
<br />
$ rm -rf git-repo1 <br />
<br />
$ git pull git-repo2 <br />
$ rm -rf git-repo2 <br />
<br />
$ git pull git-repo3 <br />
$ rm -rf git-repo3 <br />
</pre><br />
<br />
* Done! Add the remote repo info if necessary.<br />
<br />
= Migration from SVN to Git =<br />
== Install git-svn ==<br />
$ apt-get install git-svn <br />
<br />
== Prepare for Migration ==<br />
In some dir, create users.txt having all the user info<br />
e.g.)<br />
USERNAME = FirstName LastName <user@host><br />
<br />
== Migrate ==<br />
=== Without Tags ===<br />
$ git svn clone --stdlayout --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project-tmp <br />
(https://svn.repo/some-parent-folder/project-folder <- Not trunk)<br />
<br />
$ cd project-tmp <br />
<br />
$ git svn fetch <br />
<br />
* To view the other SVN branches<br />
$ git branch -r<br />
<br />
$ cd ..<br />
<br />
$ git clone project-tmp project <br />
$ rm -Rf project-tmp <br />
<br />
$ cd project <br />
$ git remote rm origin <br />
<br />
* Before this, add git repo for the project first.<br />
$ git remote add origin git@git-repo:project <br />
<br />
* To view all remote<br />
$ git remote -v <br />
<br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
<br />
<br />
* add user info to project/.git/config<br />
[user]<br />
email = user@host<br />
<br />
$ git push -u origin master <br />
<br />
* Check status<br />
$ git status <br />
# On branch master<br />
nothing to commit (working directory clean)<br />
<br />
<br />
=== With Tags ===<br />
If there are nested tag folders like <code>tags/released/1.x/1.1.0, tags/released/1.x/1.2.0, tags/released/1.x/1.3.0, tags/released/2.x/2.0.1 and tags/released/2.x/2.2.0</code>, add <code>--tags</code> (for branches, it's <code>--branches</code>)<br />
$ git svn clone --stdlayout --tags=tags/*/*/* --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project <br />
$ cd project<br />
$ git svn fetch <br />
<br />
<pre><br />
#### not necessary as it will be handled differently (by create tag and remove the svn tag) <br />
$ cp -Rf .git/refs/remotes/tags/* .git/refs/tags/ <br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ cp -Rf .git/refs/remotes/* .git/refs/heads/ <br />
$ rm -Rf .git/refs/remotes <br />
###################################################################################<br />
</pre><br />
<br />
$ git branch -r<br />
Then for each tag listed do:<br />
<br />
$ git tag tagname tags/tagname<br />
$ git branch -r -d tags/tagname<br />
<br />
$ git tag tag-name-for-git tags/tag-name-from-svn<br />
$ git branch -r -d tags/tag-name-from-svn<br />
<br />
e.g.)<br />
$ git tag 0.1.15 tags/released/0.x/some-app-0.1.15<br />
$ git branch -r -d tags/released/0.x/some-app-0.1.15<br />
<br />
* list tags<br />
$ git tag -l<br />
<br />
* remove old svn tags<br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ git remote add origin git@git-repo:project <br />
<br />
* add user info to project/.git/config <br />
$ git config -e<br />
<br />
[user]<br />
email = user@host<br />
<br />
and remove [svn-remote "svn"] and [svn]<br />
$ git push origin --all <br />
<br />
It doesn't seem like the tags are pushed?<br />
$ git push origin --tags</div>Kevinhttp://kevinlee.io/wiki/GitGit2013-09-13T14:14:41Z<p>Kevin: </p>
<hr />
<div>[[Category:Revision Control]]<br />
= Git =<br />
== Client-side Installation ==<br />
<br />
In the client,<br />
$ apt-get install git-core <br />
<br />
== Configuration ==<br />
=== User Info ===<br />
$ git config --global user.name "GivenName Surname" <br />
$ git config --global user.email user@email.address <br />
<br />
=== Default Text Editor ===<br />
$ git config --global core.editor "sublime-text -w"<br />
<br />
=== External Diff Tool ===<br />
==== Meld As External Diff Tool ====<br />
* To use Meld as a diff tool, install Meld<br />
$ sudo apt-get install meld <br />
* Set it as the default diff tool<br />
$ git config --global diff.external meld <br />
<br />
* It doesn't work and gives the following error messages.<br />
<pre><br />
$ git diff <br />
Usage: <br />
meld Start with an empty window<br />
meld <file|dir> Start a version control comparison<br />
meld <file> <file> [<file>] Start a 2- or 3-way file comparison<br />
meld <dir> <dir> [<dir>] Start a 2- or 3-way directory comparison<br />
meld <file> <dir> Start a comparison between file and dir/file<br />
<br />
meld: error: too many arguments (wanted 0-4, got 7)<br />
</pre><br />
<br />
* Create ~/diff.py<br />
<pre><br />
#!/usr/bin/python<br />
<br />
import sys<br />
import os<br />
<br />
os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))<br />
</pre><br />
* Make it executable<br />
$ chmod a+x ~/diff.py <br />
* Set the script as the diff tool<br />
$ git config --global diff.external ~/diff.py <br />
* Now it works<br />
$ git diff <br />
$ git diff HEAD <br />
<br />
<br />
==== Meld as Diff Tool ====<br />
* This way is much easier.<br />
$ git config --global diff.tool meld <br />
<br />
$ git difftool<br />
<br />
==== Meld as Diff Tool ====<br />
$ git config --global diff.tool vimdiff <br />
<br />
<br />
=== Unset External Diff Tool ===<br />
$ git config --global --unset diff.external <br />
<br />
<br />
=== Commit Template ===<br />
Create a template file<br/><br />
e.g.) $HOME/git/.your-config/git-commit-comment-template.txt<br />
CLOSED - #TICKET_NUMBER: Summary<br />
TICKET_URI<br />
Details<br />
<br />
$ git config --global commit.template $HOME/git/.your-config/git-commit-comment-template.txt <br />
<br />
<br />
=== Etc. ===<br />
$ git config --global color.ui true<br />
<br />
== Server-side Installation ==<br />
In the server,<br />
$ apt-get install git <br />
<br />
= Gitolite =<br />
== Add User to Manage Gitolite ==<br />
# Add user to manage gitolite <br />
<br />
$ adduser \<br />
--system \<br />
--shell /bin/bash \<br />
--gecos 'git version control' \<br />
--group \<br />
--disabled-password \<br />
--home /home/git \<br />
git<br />
<br />
Adding system user `git' (UID 108) ...<br />
Adding new group `git' (GID 110) ...<br />
Adding new user `git' (UID 108) with group `git' ...<br />
Creating home directory `/home/git' ...<br />
<br />
== Create SSH key ==<br />
$ ssh-keygen -t rsa <br />
/home/username/.ssh/some_git_rsa<br />
[ENTER]<br />
[ENTER]<br />
<br />
== Transfer SSH Keys to Server ==<br />
$ ssh-copy-id '-p PORT_NUMBER -i /home/user/.ssh/some_git_rsa.pub git_user@host' <br />
If it doesn't work because the user is the only user who can access the server with his/her ssh key so accessing with git_user doesn't work.<br />
<br />
$ scp -P PORT ~/.ssh/some_git_rsa.pub user@host: <br />
<br />
Log in with the user account<br />
$ ssh -p PORT user@host<br />
$ mv some_git_rsa.pub git.pub <br />
<br />
== Setup ==<br />
$ su - git <br />
$ cd ~ <br />
$ echo "PATH=$HOME/bin:$PATH" >> ~/.bashrc <br />
<br />
* don't need these 2 lines<br />
$ mkdir .ssh <br />
$ cat /home/user/git.pub >> /home/git/.ssh/authorized_keys <br />
<br />
$ exit<br />
<br />
== Gitolite Installation ==<br />
$ sudo -i <br />
$ apt-get install gitolite<br />
[ENTER] -> :q to exit<br />
$ exit<br />
<br />
== Setup git User ==<br />
$ su - git<br />
$ gl-setup /home/user/git.pub <br />
<br />
$ vim /etc/ssh/sshd_config <br />
Add git to the allowed user<br />
<br />
AllowUsers user git<br />
<br />
Restart ssh<br />
$ service ssh restart <br />
<br />
== Test ==<br />
* Testing (from user local machine)<br />
$ ssh -p PORT_NUMBER git@host <br />
Having many keys?<br />
$ ssh -p PORT_NUMBER -i ~/.ssh/user@host1.pub git@host <br />
<br />
For debugging<br />
$ ssh -p PORT_NUMBER -vT git@host <br />
<br />
$ mkdir some-dir <br />
$ cd some-dir <br />
$ git clone git@host:gitolite-admin <br />
Or if you use a specific port,<br />
$ git clone ssh://git@host:PORT_NUMBER/gitolite-admin<br />
<br />
== Add Git User (through Gitolite) ==<br />
Adding User<br />
$ cd gitolite-admin/keydir <br />
$ mkdir user_name <br />
$ cp -p ~/.ssh/user@host1.pub .<br />
$ cp -p ~/.ssh/user@host2.pub .<br />
$ git add user@host1.pub <br />
$ git commit -a -m "users added: user@host1 and user@host2" <br />
$ git push<br />
<br />
If directly copied to <code>/home/git/.gitolite/keydir</code>, run<br />
$ gl-setup<br />
to apply the changes<br />
<br />
= Usage =<br />
== Initializing a Repository in an Existing Directory ==<br />
* To make a directory a git repository, go to the project directory and run the following command<br />
<pre><br />
$ git init <br />
</pre><br />
Then it will create a new directory named <code>.git</code> inside the project directory. The <code>.git</code> folder contains all necessary files for the git repository.<br />
<br />
== Host Info Config using SSH_Config ==<br />
Create <code>~/.ssh/config</code> file and edit.<br />
<br />
Host host.name<br />
Hostname host.name<br />
Port 1111<br />
<br />
Host another.host.name<br />
Hostname another.host.name<br />
Port 2222<br />
<br />
Host host1<br />
Hostname host.name<br />
Port 1111<br />
User git<br />
IdentityFile ~/.ssh/git_rsa<br />
<br />
Host user-host2<br />
Hostname host2.name<br />
Port 1234<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
Host user-host3<br />
Hostname host3.com<br />
Port 1122<br />
User user<br />
IdentityFile ~/.ssh/user@localhost1<br />
<br />
'''IdentityFile is a private key'''<br />
<br />
To use this info,<br />
$ git clone git@host1:repo-name <br />
# it will use 'git' as a user and access the server 'host.name' using the port nubmer 1111.<br />
<br />
$ git clone git@host1:repo-name <br />
# it will use 'user' as a user and access the server 'host2.name' using the port nubmer 1234.<br />
<br />
== Push to Specific Host ==<br />
<br />
$ git push git@host1:repo-name master<br />
<br />
== Set up Project Specific User Info ==<br />
* Move to the project repository<br />
$ git config user.email user@email.address <br />
<br />
To see and edit all the config details<br />
$ git config -e<br />
<br />
== .gitignore ==<br />
To exclude files when committing, create .gitignore file and put in the project root (git project repo root).<br />
For maven project, the content should be<br />
target/<br />
<br />
To set up the global ignore file<br />
$ git config --global core.excludesfile $HOME/git/.your-config/.global-gitignore <br />
<br />
Other gitignore files,<br />
https://github.com/github/gitignore<br />
<br />
<br />
=== .gitignore for Eclipse and Maven (for Kevin) ===<br />
<pre><br />
target/<br />
<br />
*.pydevproject<br />
.project<br />
.classpath<br />
.settings/<br />
.metadata<br />
bin/<br />
tmp/<br />
*.tmp<br />
*.bak<br />
*.swp<br />
*~.nib<br />
local.properties<br />
.loadpath<br />
<br />
# External tool builders<br />
.externalToolBuilders/<br />
<br />
# Locally stored "Eclipse launch configurations"<br />
*.launch<br />
<br />
# CDT-specific<br />
.cproject<br />
<br />
# PDT-specific<br />
.buildpath<br />
</pre><br />
<br />
<br />
== Commit ==<br />
* Add file<br />
$ git add fileName<br />
<br />
* Commit<br />
$ git commit -m "comment"<br />
<br />
* Add all the files and folders<br />
$ git add -A <br />
# It is equivalent to git add .; git add -u<br />
# git add . -> add all changed and new ones but not removed ones<br />
# git add -u -> changed and removed but not new ones.<br />
<br />
=== Amend Last Comment ===<br />
* Amend the last comment<br />
$ git commit --amend -m "New comment"<br />
<br />
<br />
== Push ==<br />
=== Add Remote ===<br />
* To view all remote<br />
<pre><br />
$ git remote -v <br />
</pre><br />
<br />
* Add a remote<br />
<pre><br />
$ git remote add origin git@git-repo:project <br />
</pre><br />
Then<br />
<pre><br />
$ git push -u origin master <br />
</pre><br />
Or<br />
<pre><br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
</pre><br />
=== Push to Remote ===<br />
* Push to the remote git repository<br />
$ git push<br />
<br />
* If it gives error messages like:<br />
No refs in common and none specified; doing nothing.<br />
Perhaps you should specify a branch such as 'master'.<br />
(It doesn't happen if you have already done <code>git push -u origin master </code>).<br />
* Do<br />
$ git push origin master<br />
<br />
== Revert ==<br />
=== Reset ===<br />
* Revert to last commit.<br />
$ git reset --hard HEAD <br />
Or to specific commit/tag/branch<br />
$ git reset --hard <tag/branch/commit id> <br />
<br />
* Check the id using git log<br />
$ git log <br />
<br />
* To push it<br />
$ git push <reponame> -f <br />
<br />
=== Check out ===<br />
* Or change the name of master branch then check out from the commit to be used and make it master<br />
<br />
# Rename the current master branch<br />
#: <pre>$ git branch -m another_name_for_master </pre> <br />
# Check out from another commit:<br />
#: <pre> $ git checkout a1b2c3a3blah </pre><br />
# Make it the new master branch:<br />
#: <pre> $ git checkout -b master </pre><br />
* If the old master is no longer needed and has to be removed, remove it by<br />
$ git branch -D another_name_for_master<br />
<br />
<br />
=== Restore Removed File ===<br />
'''This has to be tested first then should be rewritten'''.<br /><br />
Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.<br />
$ git rev-list -n 1 HEAD -- <file_path> <br />
<br />
Then checkout the version at the commit before.<br />
$ git checkout <deleting_commit>^ -- <file_path> <br />
<br />
Or in one command, if $file is the file.<br />
$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"<br />
* Reference<br />
[http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo?answertab=votes#tab-top]<br />
<br />
<br />
== Branch ==<br />
* Create branch<br />
<pre><br />
$ git branch branch_name <br />
</pre><br />
* Change to the branch just created<br />
<pre><br />
$ git checkout branch_name <br />
</pre><br />
OR<br />
* Create and change<br />
<pre><br />
git checkout -b branch_name <br />
</pre><br />
<br />
* Back to master<br />
<pre><br />
$ git checkout master <br />
</pre><br />
<br />
* To push,<br />
<pre><br />
$ git push origin branch_name <br />
</pre><br />
<br />
== Tag ==<br />
=== List Tags ===<br />
<pre><br />
$ git tag <br />
</pre><br />
<br />
=== Create Tag ===<br />
* Create Tag<br />
<pre><br />
$ git tag -a tag_name -m 'tag message: new version blah blah' <br />
</pre><br />
* Tag later <br />
<pre><br />
$ git tag -a tag_name -m 'tag message' commit_checksum <br />
</pre><br />
e.g.)<br />
<pre><br />
$ git tag -a v1.5 -m 'new version with blah blah' b015e9d <br />
</pre><br />
* To see the list of checksum use the following command.<br />
<pre><br />
$ git log --pretty=oneline <br />
</pre><br />
<br />
=== Remove Tag ===<br />
<pre><br />
$ git tag -d tag_name <br />
</pre><br />
<br />
<br />
<br />
== Merge Multiple Git Repositories into One ==<br />
If the directory structures look like this<br />
<pre><br />
parent-dir (not git repo)<br />
│<br />
├─git-repo1 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
├─git-repo2 ## each is connected to its own remote git repository<br />
│ ├─src<br />
│ └.git<br />
│<br />
└─ git-repo3 ## each is connected to its own remote git repository<br />
├─src<br />
└.git<br />
</pre><br />
<br />
To make it like<br />
<pre><br />
parent-dir (now it is git repo)<br />
├─src ## contains all the files and sub-dirs from those three repositories (git-repo1, git-repo2 and git-repo3).<br />
└.git<br />
<br />
</pre><br />
and it still keeps all the histories from all those repositories, do following instructions.<br />
<br />
* Initialize the new repository directory.<br />
<pre><br />
$ cd parent-dir <br />
$ git init <br />
</pre><br />
<br />
* Pull the old ones and remove these one by one.<br />
<pre><br />
$ git pull git-repo1 <br />
<br />
## If there is any conflict, solve it first then<br />
## commit the changes<br />
$ git add file <br />
$ git commit -a <br />
## Now pull the one had conflict issue again.<br />
$ git pull git-repo1 <br />
<br />
$ rm -rf git-repo1 <br />
<br />
$ git pull git-repo2 <br />
$ rm -rf git-repo2 <br />
<br />
$ git pull git-repo3 <br />
$ rm -rf git-repo3 <br />
</pre><br />
<br />
* Done! Add the remote repo info if necessary.<br />
<br />
= Migration from SVN to Git =<br />
== Install git-svn ==<br />
$ apt-get install git-svn <br />
<br />
== Prepare for Migration ==<br />
In some dir, create users.txt having all the user info<br />
e.g.)<br />
USERNAME = FirstName LastName <user@host><br />
<br />
== Migrate ==<br />
=== Without Tags ===<br />
$ git svn clone --stdlayout --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project-tmp <br />
(https://svn.repo/some-parent-folder/project-folder <- Not trunk)<br />
<br />
$ cd project-tmp <br />
<br />
$ git svn fetch <br />
<br />
* To view the other SVN branches<br />
$ git branch -r<br />
<br />
$ cd ..<br />
<br />
$ git clone project-tmp project <br />
$ rm -Rf project-tmp <br />
<br />
$ cd project <br />
$ git remote rm origin <br />
<br />
* Before this, add git repo for the project first.<br />
$ git remote add origin git@git-repo:project <br />
<br />
* To view all remote<br />
$ git remote -v <br />
<br />
$ git config branch.master.remote origin <br />
$ git config branch.master.merge refs/heads/master <br />
<br />
<br />
* add user info to project/.git/config<br />
[user]<br />
email = user@host<br />
<br />
$ git push -u origin master <br />
<br />
* Check status<br />
$ git status <br />
# On branch master<br />
nothing to commit (working directory clean)<br />
<br />
<br />
=== With Tags ===<br />
If there are nested tag folders like <code>tags/released/1.x/1.1.0, tags/released/1.x/1.2.0, tags/released/1.x/1.3.0, tags/released/2.x/2.0.1 and tags/released/2.x/2.2.0</code>, add <code>--tags</code> (for branches, it's <code>--branches</code>)<br />
$ git svn clone --stdlayout --tags=tags/*/*/* --no-metadata -A users.txt --username=USERNAME https://svn.repo/some-parent-folder/project-folder project <br />
$ cd project<br />
$ git svn fetch <br />
<br />
<pre><br />
#### not necessary as it will be handled differently (by create tag and remove the svn tag) <br />
$ cp -Rf .git/refs/remotes/tags/* .git/refs/tags/ <br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ cp -Rf .git/refs/remotes/* .git/refs/heads/ <br />
$ rm -Rf .git/refs/remotes <br />
###################################################################################<br />
</pre><br />
<br />
$ git branch -r<br />
Then for each tag listed do:<br />
<br />
$ git tag tagname tags/tagname<br />
$ git branch -r -d tags/tagname<br />
<br />
$ git tag tag-name-for-git tags/tag-name-from-svn<br />
$ git branch -r -d tags/tag-name-from-svn<br />
<br />
e.g.)<br />
$ git tag 0.1.15 tags/released/0.x/some-app-0.1.15<br />
$ git branch -r -d tags/released/0.x/some-app-0.1.15<br />
<br />
* list tags<br />
$ git tag -l<br />
<br />
* remove old svn tags<br />
$ rm -Rf .git/refs/remotes/tags <br />
<br />
$ git remote add origin git@git-repo:project <br />
<br />
* add user info to project/.git/config <br />
$ git config -e<br />
<br />
[user]<br />
email = user@host<br />
<br />
and remove [svn-remote "svn"] and [svn]<br />
$ git push origin --all <br />
<br />
It doesn't seem like the tags are pushed?<br />
$ git push origin --tags</div>Kevin