droplang Command in Linux



The droplang command in Linux removes a specific procedural language from a PostgreSQL database.

The procedural languages in PostgreSQL are used to develop complex functions that are difficult to create with SQL. However, these procedural languages can also create security vulnerabilities in the database. It is recommended to remove these languages not only to prevent security issues but also to simplify database maintenance.

Table of Contents

Here is a comprehensive guide to the options available with the droplang command −

Note − The droplang command was deprecated in PostgreSQL version 9.1. In the recent version, the procedural languages are removed using DROP EXTENSION.

Prerequisites of using the droplang Command

The droplang and DROP EXTENSION commands depend on PostgreSQL. So, before proceeding ensure that PostgreSQL is installed and configured in Linux.

To install PostgreSQL on Ubuntu, Debian, and Debian-based distributions, use −

sudo apt install postgresql postgresql-contrib

To get it on Fedora, use −

sudo dnf install postgresql postgresql-server

After installation, start the PostgreSQL service.

To verify the installation, check the version −

psql --version
Prerequisites of Using droplang Command

Syntax of droplang Command

The general syntax of the droplang command is as follows −

droplang [options] [langname] [dbname]

The [options] field specifies various options, such as connection settings. The [langname] represents the name of the procedural language to be removed, and [dbname] is the database name from which the language will be dropped.

droplang Command Options

The options used with the droplang command are listed below −

Options Description
-d (--dbname) database To specify the database name from which language is needed to be dropped
-l (--list) To display the list of installed languages
-h (--host) hostname To specify the hostname for connection
-U (--username) username To specify the username to connect as
-p (--port) port To specify the port number on which the server is listening
-W (--password) To force the password prompt

Examples of droplang Command in Linux

In this section, the usage of the droplang command will be discussed with examples −

  • Dropping a Procedural Language
  • Listing the Installed Languages

Note − The droplang command would only work if the PostgreSQL version is lower than 9.1.

Dropping a Procedural Language

To drop a procedural language plpgsql from a database mydatabase, use −

droplang plpgsql mydatabase

Similarly, other procedural languages, such as plperl, plpython, and pltcl, can also be removed using the same method.

PostgreSQL also supports many third-party languages. A list of some third-party languages is as follows −

  • pljava
  • plphp
  • pllua
  • plsh
  • plruby

Listing the Installed Languages

To list the installed procedural language in a database, use the -l option −

droplang -l mydatabase

Using DROP EXTENSION Command to Remove Procedural Languages

The latest PostgreSQL versions no longer support the droplang command. Most of the procedural languages have been made into extensions. Therefore, to remove procedural language, use the DROP EXTENSION command.

The syntax for the DROP EXTENSION is as follows −

DROP EXTENSION [IF EXISTS] ext_name [CASCADE | RESTRICT] 

The [IF EXISTS] is an optional clause, used to check whether the object exists before performing an action. The ext_name represents the extension's name, in this case, the procedural language.

In [CASCADE | RESTRICT], the CASCADE drops the dependent object on the drop language while RESTRICT prevents deleting the language if any object depends on it. The RESTRICT is the default behavior.

The psql commands can directly be executed without activating the Postgres interactive prompt using the -c option. The -c option is used to run the specified command string.

Before removing the language using DROP EXTENSION, first list the installed extensions −

psql mydatabase -c '\dx'
DROP EXTENSION Command to Remove Procedural Languages 1

The output image shows that two procedural languages are installed i.e., plperl and plpgsql.

To drop a procedural language plperl, execute −

psql mydatabase -c 'DROP EXTENSION plperl;'
DROP EXTENSION Command to Remove Procedural Languages 2

Verify the removal by listing the remaining languages −

DROP EXTENSION Command to Remove Procedural Languages 3

Note that this command will not complete the operation if any object in the database depends on the language. To remove the language and its dependent object use, the CASCADE option −

psql mydatabase -c 'DROP EXTENSION plperl CASCADE;'

Conclusion

The droplang command in Linux removes procedural language from the PostgreSQL database. Procedural languages are used to create complex functions. However, these languages also introduce vulnerabilities. These vulnerabilities can be handled by removing the languages.

Removing the procedural language simplifies the database’s maintenance and improves the security. It is important to note that the droplang command has been deprecated and replaced with the DROP EXTENSION command.

In this tutorial, we explained the droplang command, its syntax, options, and usage through examples.

Advertisements