HOME | PROJECTS | EXTRA

GNU Guix: Databases

In this post I will write about how to set up databases on a GNU Guix System. I am going to only talk about PostgreSQL and MySQL (MariaDB) because those are the only two systems I have some knowledge about. For more informations about databases on Guix, see the Database Services section in the GNU Guix manual, ask around on the IRC (#guix on irc.freenode.net) or on the help-guix mailing list.

1 Generic

To be able to specify any database service you need to include the (gnu services databases) module in your system configuration file.

(use-modules (gnu services databases))
;; or
(use-service-modules databases)

2 PostgreSQL

Setting up PostgreSQL requires paying close attention to the Guix manual. I missed a key part when reading it for the first time.

To have the PostgreSQL database service running you need to add (postgresql-service) to the list of services.

(services
 (cons*
  (postgresql-service)
  %base-services)))

But don’t reconfigure your system yet! We need to do one more thing – install the postgresql package system-wide to be able to use the psql CLI.

(use-package-modules databases)
;; ...
(packages (cons* postgresql %base-packages))

This is the thing that I missed the first time. I spent an hour trying to figure out why I couldn’t use psql only to later see the little comment in the example code in the documentation that says to install the postgresql package.

The documentation shows an example where they enter the Postgres CLI using psql -U postgres. If that doesn’t work on your machine (it didn’t on mine), you can use sudo -u postgres psql.

After logging in to the postgres database as the postgres user, it’s time to create a new user for your regular account. In this case I will create a user with the name “brown” and the password “1234”.

CREATE USER brown WITH PASSWORD '1234';

Let’s also create a database to showcase setting permissions:

CREATE DATABASE my_test_db;

To grant administrative privileges for this database to the newly created user we write:

GRANT ALL PRIVILEGES ON DATABASE my_test_db TO brown;

Replace my_test_db and brown with your database name and user name.

You can quit your session now. The next time you want to work on that database, you can do that by using the username and database name:

psql -U brown -d my_test_db

If your *nix account has the same name as the PostgreSQL user you want to connect as, the -U username part can be ommited from the above command.

3 MySQL

The process for MySQL (MariaDB) is pretty similar, but simpler yet.

Add mariadb to your system’s package list and (mysql-service) to your services. Please note that adding mariadb to your packages is very important, as (mysql-service) doesn’t provide the mysql binaries.

And that’s pretty much it. You should be able to use MySQL with the root user with an empty password. By default, MySQL runs on port 3306. To see the customization options, check the “Database Services” section in the manual.