Create a symlink (soft link) for different PostgreSQL version

One of a setback of being database administrator is writing a full path for psql command or others. Imagine you have 3 or more postgresql version running in your computer, and for each time you want to connect to database you must typing a full path command, it’s really tedious. Luckily linux provide a symlink to make this thing easier. But before that, let’s get to know first what is a soft link and what is capable of.

A symbolic or soft link is an actual link to the original file, whereas a hard link is a mirror copy of the original file. If you delete the original file, the soft link has no value, because it points to a non-existent file.

But in the case of hard link, it is entirely opposite. Even if you delete the original file, the hard link will still has the data of the original file. Because hard link acts as a mirror copy of the original file.

In a nutshell, a soft link

  • can cross the file system,
  • allows you to link between directories,
  • has different inode number and file permissions than original file,
  • permissions will not be updated,
  • has only the path of the original file, not the contents.

A hard Link

  • can’t cross the file system boundaries (i.e. A hardlink can only work on the same filesystem),
  • can’t link directories,
  • has the same inode number and permissions of original file,
  • permissions will be updated if we change the permissions of source file,
  • has the actual contents of original file, so that you still can view the contents, even if the original file moved or removed.

Above is a quick explanation of soft and hard link, now we already know what is soft link and hard link, and what they used for. But we won’t discuss much further about that, Because know we only want to use soft link to handle the problem.

Creating Symbolic Link for postgres

Assume there are three postgres version installed on your server. PostgreSQL 9.2, 9.5, and 12, usually 9.2 will be a default if you installed postgres, that mean if you type psql command on your bash it will refer to version 9.2. Here is the example:

-bash-4.2$ psql
psql (9.2.24, server 9.5.25)
WARNING: psql version 9.2, server version 9.5.
         Some psql features might not work.
Type "help" for help.

postgres=#

There is a Warning message that say we are running database server version 9.5 using psql 9.2 version, this can lead to many problem if we continue using this. To overcome this warning we can connect to the database using the appropriate psql command, like this:

-bash-4.2$ /usr/pgsql-9.5/bin/psql
psql (9.5.25)
Type "help" for help.

postgres=#

Now there are no warning message coming up. This is because we use the same psql version as database server. As we can see, we write down a full path to get that function. Writing down a full path like that is really impractical, and that is symbolic link can help you up with it. On CentOS, to create a symbolic link for psql, we can use this command:

[root@localhost ~]# ln -s /usr/pgsql-9.5/bin/psql /usr/bin/psql95

now you don’t need to write a full path again, just type psql95 and you will be connect to a database using psql 9.5 version, like this:

-bash-4.2$ psql95
psql95 (9.5.25)
Type "help" for help.

postgres=#

Same thing to do with postgresql 12 version

[root@localhost ~]# ln -s /usr/pgsql-12/bin/psql /usr/bin/psql12
[root@localhost ~]# su - postgres
Last login: Wed Sep 15 11:06:26 WIB 2021 on pts/0
-bash-4.2$ psql12 -p 7777
psql12 (12.7)
Type "help" for help.

postgres=#

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>