Next: , Previous: , Up: 服务   [Contents][Index]


12.9.11 数据库服务

The (gnu services databases) module provides the following services.

PostgreSQL

The following example describes a PostgreSQL service with the default configuration.

(service postgresql-service-type
         (postgresql-configuration
          (postgresql postgresql-10)))

If the services fails to start, it may be due to an incompatible cluster already present in data-directory. Adjust it (or, if you don’t need the cluster anymore, delete data-directory), then restart the service.

Peer authentication is used by default and the postgres user account has no shell, which prevents the direct execution of psql commands as this user. To use psql, you can temporarily log in as postgres using a shell, create a PostgreSQL superuser with the same name as one of the system users and then create the associated database.

sudo -u postgres -s /bin/sh
createuser --interactive
createdb $MY_USER_LOGIN      # Replace appropriately.
Data Type: postgresql-configuration

Data type representing the configuration for the postgresql-service-type.

postgresql

PostgreSQL package to use for the service.

port (default: 5432)

Port on which PostgreSQL should listen.

locale (default: "en_US.utf8")

Locale to use as the default when creating the database cluster.

config-file (default: (postgresql-config-file))

The configuration file to use when running PostgreSQL. The default behaviour uses the postgresql-config-file record with the default values for the fields.

log-directory (default: "/var/log/postgresql")

The directory where pg_ctl output will be written in a file named "pg_ctl.log". This file can be useful to debug PostgreSQL configuration errors for instance.

data-directory (default: "/var/lib/postgresql/data")

Directory in which to store the data.

extension-packages (default: '())

Additional extensions are loaded from packages listed in extension-packages. Extensions are available at runtime. For instance, to create a geographic database using the postgis extension, a user can configure the postgresql-service as in this example:

(use-package-modules databases geo)

(operating-system
  ...
  ;; postgresql is required to run `psql' but postgis is not required for
  ;; proper operation.
  (packages (cons* postgresql %base-packages))
  (services
    (cons*
      (service postgresql-service-type
               (postgresql-configuration
                (postgresql postgresql-10)
                (extension-packages (list postgis))))
      %base-services)))

Then the extension becomes visible and you can initialise an empty geographic database in this way:

psql -U postgres
> create database postgistest;
> \connect postgistest;
> create extension postgis;
> create extension postgis_topology;

There is no need to add this field for contrib extensions such as hstore or dblink as they are already loadable by postgresql. This field is only required to add extensions provided by other packages.

Data Type: postgresql-config-file

Data type representing the PostgreSQL configuration file. As shown in the following example, this can be used to customize the configuration of PostgreSQL. Note that you can use any G-expression or filename in place of this record, if you already have a configuration file you’d like to use for example.

(service postgresql-service-type
         (postgresql-configuration
          (config-file
           (postgresql-config-file
            (log-destination "stderr")
            (hba-file
             (plain-file "pg_hba.conf"
                         "
local	all	all			trust
host	all	all	127.0.0.1/32 	md5
host	all	all	::1/128 	md5"))
            (extra-config
             '(("session_preload_libraries"     "auto_explain")
               ("random_page_cost"              2)
               ("auto_explain.log_min_duration" "100 ms")
               ("work_mem"                      "500 MB")
               ("logging_collector"             #t)
               ("log_directory"                 "/var/log/postgresql")))))))
log-destination (default: "syslog")

The logging method to use for PostgreSQL. Multiple values are accepted, separated by commas.

hba-file (default: %default-postgres-hba)

Filename or G-expression for the host-based authentication configuration.

ident-file (default: %default-postgres-ident)

Filename or G-expression for the user name mapping configuration.

socket-directory (default: "/var/run/postgresql")

Specifies the directory of the Unix-domain socket(s) on which PostgreSQL is to listen for connections from client applications. If set to "" PostgreSQL does not listen on any Unix-domain sockets, in which case only TCP/IP sockets can be used to connect to the server.

By default, the #false value means the PostgreSQL default value will be used, which is currently ‘/tmp’.

extra-config (default: '())

List of additional keys and values to include in the PostgreSQL config file. Each entry in the list should be a list where the first element is the key, and the remaining elements are the values.

The values can be numbers, booleans or strings and will be mapped to PostgreSQL parameters types Boolean, String, Numeric, Numeric with Unit and Enumerated described here.

Scheme Variable: postgresql-role-service-type

This service allows to create PostgreSQL roles and databases after PostgreSQL service start. Here is an example of its use.

(service postgresql-role-service-type
            (postgresql-role-configuration
             (roles
              (list (postgresql-role
                     (name "test")
                     (create-database? #t))))))

This service can be extended with extra roles, as in this example:

(service-extension postgresql-role-service-type
                   (const (postgresql-role
                           (name "alice")
                           (create-database? #t))))
Data Type: postgresql-role

PostgreSQL manages database access permissions using the concept of roles. A role can be thought of as either a database user, or a group of database users, depending on how the role is set up. Roles can own database objects (for example, tables) and can assign privileges on those objects to other roles to control who has access to which objects.

名字

The role name.

permissions (default: '(createdb login))

The role permissions list. Supported permissions are bypassrls, createdb, createrole, login, replication and superuser.

create-database? (default: #f)

Whether to create a database with the same name as the role.

Data Type: postgresql-role-configuration

Data type representing the configuration of postgresql-role-service-type.

host (default: "/var/run/postgresql")

The PostgreSQL host to connect to.

log (default: "/var/log/postgresql_roles.log")

File name of the log file.

roles (default: '())

The initial PostgreSQL roles to create.

MariaDB/MySQL

Scheme Variable: mysql-service-type

This is the service type for a MySQL or MariaDB database server. Its value is a mysql-configuration object that specifies which package to use, as well as various settings for the mysqld daemon.

Data Type: mysql-configuration

Data type representing the configuration of mysql-service-type.

mysql (default: mariadb)

Package object of the MySQL database server, can be either mariadb or mysql.

For MySQL, a temporary root password will be displayed at activation time. For MariaDB, the root password is empty.

bind-address (default: "127.0.0.1")

The IP on which to listen for network connections. Use "0.0.0.0" to bind to all available network interfaces.

port (default: 3306)

TCP port on which the database server listens for incoming connections.

socket (default: "/run/mysqld/mysqld.sock")

Socket file to use for local (non-network) connections.

extra-content (default: "")

Additional settings for the my.cnf configuration file.

extra-environment (default: #~'())

List of environment variables passed to the mysqld process.

auto-upgrade? (default: #t)

Whether to automatically run mysql_upgrade after starting the service. This is necessary to upgrade the system schema after “major” updates (such as switching from MariaDB 10.4 to 10.5), but can be disabled if you would rather do that manually.

Memcached

Scheme Variable: memcached-service-type

This is the service type for the Memcached service, which provides a distributed in memory cache. The value for the service type is a memcached-configuration object.

(service memcached-service-type)
Data Type: memcached-configuration

Data type representing the configuration of memcached.

memcached (default: memcached)

The Memcached package to use.

interfaces (default: '("0.0.0.0"))

Network interfaces on which to listen.

tcp-port (default: 11211)

Port on which to accept connections.

udp-port (default: 11211)

Port on which to accept UDP connections on, a value of 0 will disable listening on a UDP socket.

additional-options (default: '())

Additional command line options to pass to memcached.

Redis

Scheme Variable: redis-service-type

This is the service type for the Redis key/value store, whose value is a redis-configuration object.

Data Type: redis-configuration

Data type representing the configuration of redis.

redis (default: redis)

The Redis package to use.

bind (default: "127.0.0.1")

Network interface on which to listen.

port (default: 6379)

Port on which to accept connections on, a value of 0 will disable listening on a TCP socket.

working-directory (default: "/var/lib/redis")

Directory in which to store the database and related files.


Next: 邮件服务, Previous: 声音服务, Up: 服务   [Contents][Index]