How to install PL/.NET

1. Requirements

Before installing pldotnet, you will need to have the following software installed on your system:

  • PostgreSQL 10 or greater
  • .NET 6.0 or greater pldotnet also requires libglib2.0 and make, which you can install using the command below.
sudo apt install -y libglib2.0 make

You can also use the ready-to-go Docker images of pldotnet. In this case, you will need:

2. Using Docker

Currently, pldotnet is available with public Docker containers and can be executed using the following instructions:

First, make sure that you are not running a local Postgres database or using the 5432 port in your machine.

2.1 Ubuntu22-based image

If you want to use an Ubuntu22 Docker image with PostgreSQL and pldotnet installed, you can run:

docker run --rm -it brickabode/pldotnet

After downloading and starting the docker container, you should see this log:

 * Starting PostgreSQL 14 database server                                                                                                                        [ OK ] 
Press <enter> for psql, 'q' to exit, or anything else for a shell

2.2 Postgres-based image

This image is built on top of the official PostgreSQL image. You will probably want to use it on your production system in your production environment (it is recommended to use the docker-compose tool. You can use it to replace any PostgreSQL image in a container composition.

To download and start the service, run:

docker run --rm  --name postgres-pldotnet -e POSTGRES_PASSWORD=password123 -p 5432:5432 brickabode/postgres-pldotnet

Then, you can connect to your database using psql:

PGPASSWORD=password123 psql -h localhost -U postgres

As this image is derived from the official Postgres image, you can refer to their official documentation for more information.

2.3 Testing pldotnet

To test pldotnet, you can easily verify its functionality by copying and pasting the following code into your psql connection:

DO $$ Elog.Info("Hello PL.NET!"); $$ LANGUAGE plcsharp;

You should see something like this:

 * Starting PostgreSQL 14 database server                                                                                                                                                            [ OK ] 
psql (14.10 (Ubuntu 14.10-0ubuntu0.22.04.1))
Type "help" for help.

postgres=# DO $$ Elog.Info("Hello PL.NET!"); $$ LANGUAGE plcsharp;
INFO:  Hello PL.NET!
DO

3. Installing .NET

.NET is a free, open-source, cross-platform framework developed by Microsoft that enables developers to build a variety of applications, including web, mobile, desktop, gaming, and IoT (Internet of Things) applications. .NET is built on top of the Common Language Runtime (CLR), which provides a runtime environment for executing code written in a variety of languages, including C#, Virtual Basic, and F#.

One of the key features of .NET is its ability to support multiple programming languages, which allows developers to choose the language that best fits their needs and expertise. .NET also includes a rich set of libraries and frameworks for building applications, including the .NET Framework, .NET Core, and ASP.NET.

For installing .NET please follow the instructions on the official Microsoft page according to your operational system.

Typically for Ubuntu 22.04, the instructions are:

# Download Microsoft's Debian package:
wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

# Install the package:
sudo dpkg -i packages-microsoft-prod.deb

# Remove the downloaded file:
rm packages-microsoft-prod.deb

# Update the package lists:
sudo apt-get update

# Soon pldotnet will support .NET 7.
# Install .NET 6:
sudo apt-get install -y dotnet-sdk-6.0 dotnet-runtime-6.0 dotnet-hostfxr-6.0

4. Installing PostgreSQL

PostgreSQL, also known as Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance. It is designed to handle a range of workloads, from single machines to data warehouses or Web services with many concurrent users. It is the default database for macOS Server, and is also available for Linux, FreeBSD, OpenBSD, and Windows.

PostgreSQL is known for its strong support for transactions and reliability, as well as its ability to handle a wide variety of data types, including JSON and arrays. It also has a large and active developer community, with many additional libraries and tools available to enhance its capabilities.

You can install PostgreSQL following the instruction on the official page according to your operational system and desired version. Here are the steps to install on Ubuntu:

# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# Update the package lists:
sudo apt-get update

# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql

5. Installing pldotnet

As already mentioned, pldotnet is a free software (Apache v2) project that extends PostgreSQL to support stored procedures and triggers for the .NET platform, including both C# and F#.

We at Brick Abode provide Debian packages for different versions of PostgreSQL on amd64 architectures.

You can choose the most suitable version of pldotnet for your needs and install it using

sudo dpkg -i postgresql-*-pldotnet_0.99-rc1_amd64.deb

Once pldotnet is installed, you can start using it to write code that interacts with your database using popular programming languages like C#. This can be a fun and rewarding experience for developers.

5.1 Building Debian packages

5.1.1 Using Docker

To create Debian packages for pldotnet using Docker, you will need to have docker and docker-compose installed on your machine. You can find instructions for installation in the oficial Docker documentation.

Before you can build the packages, you'll need to specify the version of PostgreSQL you're using in the pldotnet/debian/pgversions file. By default, the package is generated for PostgreSQL 15, so if you're using a different version, you'll need to replace 15 in the file with the version you're using.

Once you've installed docker and docker-compose and set the PostgreSQL version in the pldotnet/debian/pgversions file, you can build the pldotnet packages by following these steps:

# Clone the pldotnet repository:
git clone https://github.com/Brick-Abode/pldotnet.git

# Change into the repository directory:
cd pldotnet

# Initialize submodules
git submodule update --init --recursive

# Create a docker container to build the packages
docker-compose up pldotnet-build

The packages will be stored in the pldotnet/debian/packages directory.

If you want to try out pldotnet before installing it on your machine, you can use a Docker container by running the following commands:

# Create a docker container and open the bash terminal
docker-compose run --rm pldotnet-build bash

# Install pldotnet inside the container
dpkg -i debian/packages/postgres-15-pldotnet_0.1-1_amd64.deb

5.1.1.1 Debian packages on ARM processors

To create pldotnet packages for installation on ARM processors, you will need to specify the version of PostgreSQL you are using in the pldotnet/debian/pgversions-arm file. Then, navigate to the pldotnet directory and run the following command:

# Create a docker container to build the ARM packages
docker-compose up pldotnet-build-arm

After building the packages, they will be stored in the pldotnet/debian/packages directory. If you want to install them in a clean container, you can use the following instructions:

# Create a docker container and open the bash terminal
docker-compose run --rm pldotnet-build-arm bash

# Install pldotnet inside the container
dpkg -i debian/packages/postgresql-15-pldotnet_0.1-1_amd64.deb

5.1.2 Using your own machine

You can use your own machine to create Debian packages for pldotnet. In addition to having PostgreSQL and .NET installed, you'll need to have the following packages installed:

  • build-essential
  • devscripts
  • debhelper

You can install these packages by running the following command:

sudo apt install -y build-essential devscripts debhelper

Once you have all the required packages installed, you can create the pldotnet packages with the following commands:

# Update the control file
pg_buildext updatecontrol

# Build the Debian packages
debuild -b -uc -us --lintian-opts --profile debian

# Copy the packages to the correct directory
cp ../postgresql-*-pldotnet_*.deb debian/packages/

# Remove unnecessary files created during the build process
rm -rf ../postgresql-*-pldotnet_*.deb ../pldotnet_*.build ../pldotnet_*.buildinfo ../pldotnet_*.changes ../postgresql-*-pldotnet*.ddeb