Narendra K https://www.linuxtechi.com Fri, 28 Oct 2022 02:40:27 +0000 en-US hourly 1 https://www.linuxtechi.com/wp-content/uploads/2020/02/cropped-linuxtechi-favicon-32x32.png Narendra K https://www.linuxtechi.com 32 32 How to Install PostgreSQL 15 on Ubuntu 22.04 Step-by-Step https://www.linuxtechi.com/how-to-install-postgresql-on-ubuntu/ https://www.linuxtechi.com/how-to-install-postgresql-on-ubuntu/#comments Mon, 24 Oct 2022 16:24:20 +0000 https://www.linuxtechi.com/?p=15068 In this article, we will explain how to install PostgreSQL 15 database server on Ubuntu 22.04 (Jammy Jellyfish). PostgreSQL is a powerful, open-source object-relational Database Management System (DBMS). It’s been battle-tested for over 35 years which has earned it a strong reputation for reliability and ... Read more

The post How to Install PostgreSQL 15 on Ubuntu 22.04 Step-by-Step first appeared on LinuxTechi.]]>
In this article, we will explain how to install PostgreSQL 15 database server on Ubuntu 22.04 (Jammy Jellyfish).

PostgreSQL is a powerful, open-source object-relational Database Management System (DBMS). It’s been battle-tested for over 35 years which has earned it a strong reputation for reliability and performance. This feature-rich database is used by many tech giants, such as Apple, IMDB, Instagram, and so on.

PostgreSQL supports large number of the SQL standard and is constructed to be extensible by users in many aspects. Some of the salient features include ACID transactions, foreign keys, subqueries, triggers, user-defined types, functions, etc.

Prerequisites

Before installing the PostgreSQL server, we must ensure that the system meets the following installation requirements:

  • Pre-Installed Ubuntu 22.04
  • A regular user with sudo rights
  • An active internet connection
  • At least 2 GB of RAM with an additional 512 MB of disk space. Please note that this is a minimal requirement for the demo environment. The actual hardware configuration will vary with data volume.

Without any further delay, let’s deep dive into PostgreSQL 15 installation steps,

1) Enable PostgreSQL Package Repository

PostgreSQL 15 package is not available in the default package repository, so enable its official package repository using following commands.

$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
$ wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo tee /etc/apt/trusted.gpg.d/pgdg.asc &>/dev/null

To begin, let’s fetch the latest versions of the packages. We can achieve this using the apt update command as shown below:

$ sudo apt update

The above command will take a few seconds to complete.

2) Install PostgreSQL 15 Database Server and Client

The postgresql package installs the default version of the PostgreSQL database server whereas the postgresql-client package installs the client utility.

Let’s install the PostgreSQL client and server using the below apt command:

$ sudo apt install postgresql postgresql-client -y

Next, let’s verify that the PostgreSQL service is up and running:

$ sudo systemctl status postgresql

PostgreSQL-Service-Status-Ubuntu-Linux

Finally, check the PostgreSQL version using the psql command line utility:

$ psql --version

Here, we can see that the version of PostgreSQL is 15.

psql-version-check-ubuntu-linux

3) Update PostgreSQL Admin User Password

By default, we can connect to the PostgreSQL server without using any password. Let’s see this in action using the psql utility:

$ sudo -u postgres psql
postgres=#

In the above output, the postgres=#  prompt indicated the active connection with the PostgreSQL server.

In this example, we have used the postgres user. This is an admin user of PostgreSQL and it gets created during the installation process.

Allowing administrative access to the database without any password isn’t a good idea. So, let’s set the password for the postgres user:

postgres=# ALTER USER postgres PASSWORD 'demoPassword';

The above SQL query sets the user password to demoPassword. Please note that, we have used a very simple password because this is a demo environment. However, the same is not recommended in the production environment.

Let’s verify that the password has been set successfully. So first, terminate the current session with the server using the \q command.

postgres=# \q

Output of above commands,

Set-Admin-User-Password-PostgreSQL-Ubuntu

Now, let’s connect to the database server again:

$ psql -h localhost -U postgres

Let’s enter the demoPassword string as a password and now we are connected to the database.

psql-connect-localhost-ubuntu-linux

4) Configure PostgreSQL to Allow Remote Connections

By default, PostgreSQL accepts connections from the localhost only. However, we can easily modify the configuration to allow connection from remote clients.

PostgreSQL reads its configuration from the postgresql.conf file which is located in the /etc/postgresql/<version>/main/ directory. Here, the version indicates the major version of PostgreSQL.

For example, in our case the full path of the file is /etc/postgresql/15/main/postgresql.conf.

Now, open the postgresql.conf file in a text editor, uncomment the line that starts with the listen_addresses, and replace ‘localhost’ with ‘*’.

This setting is located under the CONNECTIONS AND AUTHENTICATION section. After modification the file will look like this:

Listen-Address-PostgreSQL-Conf-Ubuntu-Linux

Save and close the file.

Next, edit the IPv4 local connections section of the pg_hba.conf file to allow IPv4 connections from all clients. Please note that this file is also located in /etc/postgresql/15/main/ directory.

$ sudo vi /etc/postgresql/15/main/pg_hba.conf

After modification the file will look like this:

Allow-IPV4-Connections-PostgreSQL-Ubuntu-Linux

In the above configuration indicates to allow connection from the network 192.168.1.0/24

In case, Ubuntu firewall is running on your system then allow PostgreSQL 5432 port using following command,

$ sudo ufw allow 5432/tcp

Verifying Remote Connection

Finally, restart the service and verify it’s up and running:

$ sudo systemctl restart postgresql
$ sudo systemctl status postgresql

Now, let’s try to access DB from remote client.

$ psql -h 192.168.1.192 -U postgres

In this example, 192.168.1.192 is the IP address of the PostgreSQL database server.

Remote-Connection-PostgreSQL-Command

Here we can see that we are able to access DB from the remote client.

That’s all from this article.Please do post your queries and feedback in the below comments section.

Read Also: How to Set Static IP Address on Ubuntu Server 22.04

The post How to Install PostgreSQL 15 on Ubuntu 22.04 Step-by-Step first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/how-to-install-postgresql-on-ubuntu/feed/ 2
Learn Git Commands with Practical Examples on Linux – Part 2 https://www.linuxtechi.com/git-command-practical-examples-linux-part-2/ https://www.linuxtechi.com/git-command-practical-examples-linux-part-2/#respond Wed, 15 Aug 2018 13:33:40 +0000 https://www.linuxtechi.com/?p=7898 In previous article we have learned basic Git workflow. In this article we’ll be focusing on some advanced features like miscellaneous repository actions, branching and tagging. Like previous article this is also hands-on guide to Git. Miscellaneous repository actions Rename As name suggests rename operation ... Read more

The post Learn Git Commands with Practical Examples on Linux – Part 2 first appeared on LinuxTechi.]]>
In previous article we have learned basic Git workflow. In this article we’ll be focusing on some advanced features like miscellaneous repository actions, branching and tagging. Like previous article this is also hands-on guide to Git.

Git-Command-Example-Part2

Miscellaneous repository actions

Rename

As name suggests rename operation instructs Git that traced file has been modified. To rename file execute following command:

$ git mv README NEW-README

Now let us check the repository status:

$ git status -s
R  README -> NEW-README

In above output letter R appears before README, which indicates file has been renamed. Next column shows the old and new file name.

NOTE: To make these changes in remote repository execute git push command.

Move

Move operation is used to move file from one directory location to another. To move file execute following command:

$ mkdir new-dir
$ git mv README new-dir/

In this example, we have created new directory and move file to that directory. Now let us check the repository status:

$ git status -s
R  README -> new-dir/README

Above output shows that file has been moved to new directory.

NOTE: To make these changes in remote repository execute git push command.

Delete

As name suggests, delete operation removes file/directory from Git. To remove file execute following command:

$ git rm README

Now let us check the repository status:

$ git status -s
 D README

In above output letter D appears before the README which indicates that file has been removed from repository.

NOTE: To make these changes in remote repository execute git push command.

RESET

This operation reset current HEAD to the specified state. In Git, HEAD is the reference pointer which points to the latest commit.

Let us understand this with example:

$ touch AUTHORS
$ git add AUTHORS
$ git status -s
A  AUTHORS

In above example, we have created AUTHORS file and added it to changeset. Now this file will be part of next commit. Git reset command will adjust HEAD reference and remove file from changeset

$ git reset HEAD AUTHORS

Now, let us check the repository status:

$ git status -s
?? AUTHORS

As expected, above output shows that file has been removed from changeset hence ?? symbol appears before filename.

Working with branches

Branch in version control system is a independent line of development. Unlike other version control system branching is really lightweight in Git. In this section we’ll discuss various branch related features.

Create branch

To create new branch execute following command:

$ git branch my-feature-branch

In above command my-feature-branch is branch name. This command will create local branch.

To create remote branch execute git push command as follows:

$ git push origin my-feature-branch

List branch

To list branch execute branch command without any argument.

$ git branch
* master
  my-feature-branch

For instance, above output lists local branches. Asterisk symbol represents current branch. In our case it is master branch.

To list local as well as remote branches execute following command:

$ git branch -a
* master
  my-feature-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

In above output, branches highlighted in red colour are remote branches.

Delete branch

To delete local branch use -d option as follows:

$ git branch -d my-feature-branch

Deleted branch my-feature-branch (was 220bf4d).

To delete remote branch execute following command:

$ git push origin :my-feature-branch

Note that we have use colon(:) with branch name which instructs git to delete remote branch

Switch branch

To switch in between branches execute checkout command as follows:

$ git checkout my-feature-branch

Switched to branch ‘my-feature-branch’

Now let us check the current branch:

$ git branch
  master
* my-feature-branch

Checkout branch

So far we have used separate command to create and switch to branch. However, we can achieve this is single command as follows:

$ git checkout -b my-feature-branch

Switched to a new branch ‘my-feature-branch’

Now let us check the current branch:

$ git branch
  master
* my-feature-branch

Restore working tree

Git allows us to discard uncommitted changes. We can achieve this using checkout command.  Let us understand this with example.

First modify existing file and check repository status:

$ echo "Update README" >> README
$ git status -s
 M README

Now to discard changes of README file, execute following command:

$ git checkout -- README
$ git status -s

As expected our working tree is clean hence last command is not showing any output.

Merge branch

As name suggests merge operation applies changes from one branch to another. Let us understand this step by step.

First create a new branch and commit changes in this branch:

$ git checkout -b my-feature-branch

Switched to a new branch ‘my-feature-branch’

$ echo "Update README" >> README.md
$ git add README.md
$ git commit -m "Updated README"
[my-feature-branch 42e28aa] Updated README
 1 file changed, 1 insertion(+)

Let us switch to master branch and apply changes to master branch

$ git checkout master

Switched to branch ‘master’

Your branch is up to date with ‘origin/master’.

$ git merge my-feature-branch
Updating 220bf4d..42e28aa
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

Now let us verify that changes are applied to master branch:

$ git log -1 --oneline
42e28aa (HEAD -> master, my-feature-branch) Updated README

Working with tags

Tag is a reference pointer to a particular commit in Git history. Often tag is used to mark release version of product. In this section we’ll discuss basic tag operations like create, list and delete

Create tag

Creating tag is really simple. To create tag execute following command:

$ git tag my-tag

Above command will create tag with name  my-tag. To create tag in remote repository execute push command as follows:

$ git push origin my-tag

List tag

To list tag execute Git tag command without any argument as follows:

$ git tag
my-tag

Delete tag

To delete tag execute Git tag command with -d argument as follows:

$ git tag -d my-tag
Deleted tag 'my-tag' (was 220bf4d)

To delete tag from remote repository execute following command:

$ git push origin :my-tag

Conclusion

In this article we have discussed some advanced Git operations with example. After referring this tutorial you can use Git fluently in your project. If you do like the article, please share your feedback and comments.

The post Learn Git Commands with Practical Examples on Linux – Part 2 first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/git-command-practical-examples-linux-part-2/feed/ 0
Learn Git Command with Practical Examples on Linux – Part 1 https://www.linuxtechi.com/learn-git-command-examples-linux-part-1/ https://www.linuxtechi.com/learn-git-command-examples-linux-part-1/#comments Thu, 02 Aug 2018 05:11:58 +0000 https://www.linuxtechi.com/?p=7878 Are you looking for hands on guide on git command ? This tutorial will cover git command with practical examples on Linux. We are assuming that Git is installed on your system. Git is distributed version control system. It is primarily used by software developers ... Read more

The post Learn Git Command with Practical Examples on Linux – Part 1 first appeared on LinuxTechi.]]>
Are you looking for hands on guide on git command ?

This tutorial will cover git command with practical examples on Linux. We are assuming that Git is installed on your system.

Git is distributed version control system. It is primarily used by software developers for their source code management. Git is free and open source software and many large organizations use it to manage their huge code base.

Git User Initial Configuration

 

First we have to configure settings for Git user. We can make these settings for all repositories present on current system or for a particular repository. Let us understand this with example:

User identity

In Git we can specify user identity by providing its name and e-mail address. This information will be used during each commit operation. Execute below command in terminal to assign identity:

$ git config --global user.name "Linuxtechi User"
$ git config --global user.email "linuxtechiuser@linuxtechi.com"

Editor

This setting configures editor, which will be used while providing commit message:

$ git config --global core.editor vim

Compression

This setting configures compression level to be used. Valid range for compression is -1 to 9. -1 value indicates zlib compression and is default compression level. 0 value means no compression, and 1 to 9 are various speed/size tradeoffs, 9 being slowest.

$ git config --global core.compression 2

Diff tool

This setting configures diff viewer tool. For example, below command configures vimdiff as a diff tool:

$ git config --global diff.tool vimdiff

In above commands we have used –global option everywhere, which will make this configuration global. It means that same configuration will be applied to all repositories present on current system. To make configuration repository specific just remove –global option.

List configuration

To list Git configuration execute below command in terminal:

$ git config -l

This command will generate following output:

core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.editor=vim
core.compression=2
user.name=Linuxtechi User
user.email=linuxtechiuser@linuxtechi.com
diff.tool=vimdiff

Git repositories

Repository is a location where source code is stored. We can either create new repository or use existing repository. To create new empty repository execute below command:

$ mkdir my-repo.git
$ cd my-repo.git
$ git init --bare
Initialized empty Git repository in my-repo.git

This method will be useful when you are starting with new project. Another method is to use existing repository. Such a repository is hosted on remote server like GitHub. To download remote repository use clone command as follows:

$ git clone https://github.com/linuxtechiuser/my-repo.git

In above command last argument is path of remote repository.

Git workflow

In this section we’ll discuss git workflow.

Introduce new changes

First step is to introduce new changes. It can be addition of new file or updating existing files. Let us create a new file and modify existing file

$ touch AUTHORS                                  # Create new file
$ echo "New Contents" >> README                  # Update existing file

Check repository status

Git is content tracking system, it will identify above two changes. Let us check repository status:

$ git status -s
 M README
?? AUTHORS

In above output letter M appears before README which indicates that existing file is modified. Whereas ?? appears before AUTHORS which indicates that this is new file and Git is not aware about it hence such a file is called untracked file.

Add file to changeset

Let us instruct Git to track this new file. We can achieve this using add command. This operation will start tracking changes made to this file.

$ git add AUTHORS

Let us check repository status now:

$ git status -s
A  AUTHORS
M README

Above output shows A before AUTHORS which indicates that this file is newly added under Git. We can add any number of files using this command.

Remove file from changeset

Let us instruct Git to untrack this newly added file. We can achieve this using reset command. This operation will remove file from changeset

$ git reset AUTHORS
$ git status -s
 M README
?? AUTHORS

Above output shows that AUTHORS file is not tracked by Git.

Commit changes

In Git, files which are part of changeset will form a commit. Each commit will get unique ID. Let us create changeset first

$ git add AUTHORS
$ git add README

Now let us commit changes to local repository with commit message. In below command -m argument indicates commit message.

$ git commit -m "Updated README and added AUTHORS"

When you execute above command it will generate following output:

[master 0b124eb] Updated README and added AUTHORS
 2 files changed, 1 insertion(+)
 create mode 100644 AUTHORS

Review changes

In this section we’ll discuss commands which will allow us to review repository changes.

View commit log

Repository can contain multiple commits by multiple authors. We can use log command to view all available commits:

$ git log

When you execute above command it will generate following output:

commit 0b124eb6d0109d837f6f9396c9937406abd3f456 (HEAD -> master)
Author: Linuxtechi User <linuxtechiuser@linuxtechi.com>
Date:   Fri Jul 27 21:06:55 2018 +0530

    Updated README and added AUTHORS

This is commit we had created earlier. In above command:

  • Hexadecimal ID represents a commit ID
  • Author section of commit shows details about who made these changes
  • Date section shows date and timestamp of commit

View short commit log

Above command will show detailed information about each commit. To view short description about each commit use –oneline option as follows:

$ git log --oneline

When you execute above command, it will generate following output:

0b124eb (HEAD -> master) Updated README and added AUTHORS

View commit

Commit ID is associated with each changeset. We can use this ID with show command to view commit contents.

$ git show 0b124eb6d0109d837f6f9396c9937406abd3f456

When you execute above command it will generate following output:

commit 0b124eb6d0109d837f6f9396c9937406abd3f456 (HEAD -> master)
Author: Linuxtechi User <linuxtechiuser@linuxtechi.com>
Date:   Fri Jul 27 21:06:55 2018 +0530
    Updated README and added AUTHORS
diff --git a/AUTHORS b/AUTHORS
new file mode 100644
index 0000000..e69de29
diff --git a/README b/README
index 980a0d5..5680123 100644
--- a/README
+++ b/README
@@ -1 +1,2 @@
 Hello World!
+New Contents

View diff

Diff command allows us to review changes before creating changeset. Diff command shows the differences between repository and local workspace. Let us modify README file and view differences

$ echo "Generating diff" >> README
$ git diff
diff --git a/README b/README
index 5680123..3158230 100644
--- a/README
+++ b/README
@@ -1,2 +1,3 @@
 Hello World!
 New Contents
+Generating diff

In above command:

  • + sign indicates changes which are added to file
  • – sign indicates changes which are removed from file

Working with Remote Repositories

So far we were working with local repository only. Other developers cannot use changes which are made by us as they are local. Hence code collaboration is not possible. This section will describe commands which will allow us to interact with remote repositories

Publish changes to remote repository

We can publish local changes to remote repository so that other developers can use it. Before publishing changes we have to create changeset and local commit. We can publish changes using push command as follows:

$ git push

This command copies changes from local workspace to remote repository. In Git, this operation is referred as push operation.

Sync workspace with remote repository

Many developers can publish their workspace changes to remote repository. Git allows us to download those changes and sync workspace with repository. We can achieve this using pull command:

$ git pull

In Git, this operation is referred as pull operation.

Miscellaneous Git commands

This section discusses miscellaneous Git commands which will be useful to perform day-to-day tasks:

Modify latest commit

Git allows us to modify latest commit. We can use this method to avoid creation of new commit. It is mostly used to modify previous commit message. To modify latest commit use –amend option as follows:

$ echo "Yet another new change" >> README
$ git add README
$ git commit --amend -m "This is amended commit

Now let us check the commit log:

$ git log
commit 8bf67aec1d1de87f03ab6aae93940b17826fde1c (HEAD -> master)
Author: Linuxtechi User <linuxtechiuser@linuxtechi.com>
Date:   Fri Jul 27 21:54:55 2018 +0530

    This is amended commit

If you observe above output carefully then we can see new commit message, its ID and new timestamp.

Remove untracked files

Untracked files are those which are unknown to Git. We can remove all untracked files using clean command.

Let us create few untracked files:

$ touch delete-me-1 delete-me-2 delete-me-3

To remove all above untracked file use clean command with -f option as follows:

$ git clean -f
Removing delete-me-1
Removing delete-me-2
Removing delete-me-3

Please note that this command will remove files permanently hence use it with caution.

View commits of particular author

If we use log command then it shows commits of all authors. To view commits of particular author use –author flag as follows:

$ git log --author=Linuxtechi

When you execute above command it will list all the commits of Linuxtechi authors as follows:

commit 8bf67aec1d1de87f03ab6aae93940b17826fde1c (HEAD -> master)
Author: Linuxtechi User <linuxtechiuser@linuxtechi.com>
Date:   Fri Jul 27 21:54:55 2018 +0530

    This is amended commit

View history of each file line by line

To view line by line history we can use blame command.

$ git blame README

When you execute above command it will generate following output:

76294131 (Linuxtechi User         2018-07-27 21:12:11 -0700 1) Hello World!
8bf67aec (Linuxtechi User         2018-07-27 21:54:55 +0530 2) New changes
8bf67aec (Linuxtechi User         2018-07-27 21:54:55 +0530 3) Yet another changes

In above command:

  • First column indicates commit ID
  • Second column indicates author
  • Third column indicates timestamps
  • Last column indicates line number and file content

View diff from staging area

When you create changeset using add command then file is logically moved to staging area. Let us see with this example:

$ echo "Let us demonstrate staging area" >> README
$ git add README
$ git diff

Above command will not show any difference as file is move to staging area. Let us use –staged operation to view differences:

$ git diff --staged
diff --git a/README b/README
index 74729a2..8bc5ffd 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
 Hello World!
 New changes
 Yet another changes
+Let us demonstrate staging area

Conclusion

In this tutorial we discussed basic operations of Git with simple examples. This tutorial is good starting point for Git newbies.

Read More: Learn Git Command with Practical Examples on Linux – Part 2

The post Learn Git Command with Practical Examples on Linux – Part 1 first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/learn-git-command-examples-linux-part-1/feed/ 1