PostgreSQL data directory configuration - postgresql

I am using PostgreSQL for CentOS. And i changed the data directory to store PostgreSQL data on a different disk.
nano /usr/lib/systemd/system/postgresql.service
#Environment=PGDATA=/var/lib/pgsql/data
Environment=PGDATA=/data/pgsql/data
However, after installing the package update, the contents of the configuration file were changed back to the default settings.
Do I need to check the configuration file every time I install a package update later? Or is there a way to preserve the config file?

There are two ways to deal with this:
the old way:
You create a file /etc/systemd/system/postgresql.service that contains
.include /usr/lib/systemd/system/postgresql.service
[Service]
Environment=PGDATA=/data/pgsql/data
the new way:
You create a directory /etc/systemd/system/postgresql.service.d that contains a file named (for example) pgdata.conf with the contents
[Service]
Environment=PGDATA=/data/pgsql/data
Then notify systemd with
systemctl daemon-reload
This configuration change will override the corresponding value from /usr/lib/systemd/system/postgresql.service, so the change will survive an upgrade.

Related

How to change MariaDB's data directory?

I use CentOS7 as my system
I tried to change the data direction on MariaDB 10.1.43
I follow the process on internet and all show to change the datadir=/var/lib/mysql/ in my.cnf
but the problem is there is no my.cnf file in my computer
only a my.cnf.d folder with a server.cnf file in it
I type datadir=/newpath/ in this server.cnf
but it didn't work, the datadir that mariaDB shows is still /var/lib/mysql/
what should I do for now? how can I find this my.cnf file?
I realize this is an old question. But wanted to add an answer that worked for me on a legacy machine running MariaDB 10.1.43 and CentOS 6.
Within the /etc/my.cnf file add this under [client-server] to look like this:
[client-server]
port=3306
socket=/home/mysql/mysql.sock
Then, within the /etc/my.cnf.d folder in the server.cnf file add this under [mysqld] to look like this:
[mysqld]
datadir=/home/mysql
socket=/home/mysql/mysql.sock
I moved the data to the /home directory, which is a newly mounted volume with additional space for this machine.
The next part of my answer is out of scope for this question. But the instructions here worked like a charm for moving your MySQL/MariaDB data directory. Semi-pro tip: Be sure to follow the RedHat/CentOS step to add the security context.

"mount" a PostgreSQL database from files not Backup

I've been given a project to extract data from a PostgreSQL database. I've no previous experience with PostgreSQL but the project I have is to bug fix existing code, so all the logic to connect to the engine and get data is already in place.
The problem I have is the database has been given to me in the form of the folders and files straight from the source HDD, not a backup (which isn't going to happen so "Get the customer to give you a backup instead isn't an option here).
The folders also contained the actual PostgreSQL binaries so I looked a the version (9.4.14) and downloaded the nearest (9.4.18) from the PostgreSQL site and installed it. Now all I have to do is some how is to get it to look at my given data files.
I tried the obvious of copying the contents of the data folder into the installed data folder but after the PostgreSQL service won't start.
I did find a option in the conf file:
#data_directory = 'ConfigDir'
I changed this to:
data_directory = 'C:\customer\data'
But again the service won't start after this.
The data directory used by the service is defined through the service command line which overwrites any property defined in postgresql.conf.
You need to re-create the service in order to change the data directory, e.g.:
Remove the service:
pg_ctl -unregister -N postgresql-9.1
postgresql-9.1 is the "real" name of the service, not the "Display Name". You can see that in the properties of the service inside the "services" app.
Then re-create the service with the correct data directory:
pg_ctl -register -D -D c:\customer\data -N postgresql-9.1
Another way of "debugging" startup errors in Windows, is to start Postgres from the command line (not through the service) because some errors during startup are not logged in the Postgres logfile but they are displayed on the command line. You can do that with e.g.:
pg_ctl start -D c:\customer\data`
If the bin directory is not in your PATH you need to specify the full path to it on the command line, e.g.: c:\Postgres9.1\bin\pg_ctl

Mounting pgsql volume in Docker

I have a custom Dockerfile based on centos:latest that installs software I need to run the web framework I use. I want to extend this Dockerfile so that I can add PostgreSQL, the catch being that I want to change pgsql's default data location from /var/lib/pgsql to /var/www/data/pgsql, which is a filesystem-mounted volume. I'm not sure if it is possible to do this in the Dockerfile or if I need to run some script afterwards from within a container. Any help would be appreciated.
Ultimately you need to change data_directory in postgresql.conf. The default location for this in postgresql 9.5 is /etc/postgresql/9.5/main/postgresql.conf.
This might look like:
Keep your own postgresql.conf file in version control, with data_directory = '/var/www/data/pgsql'
In your Dockerfile, install postgresql
In your Dockerfile, COPY postgresql.conf /etc/postgresql/9.5/main/postgresql.conf, which will move your altered postgresql.conf into the container, and replace the default postgresql.conf. This is assuming PG 9.5 default install location in the container.
Now when you run postgres, it should use your conf file with the /var/www/data/pgsql data directory.
edit: about moving data_directory

Missing mongodb.conf on FreeBSD 10.2

After having installed MongoDB 2.6.12 for FreeBSD 10.2, I need to change some MongoDB configuration parameters but there seems to be a problem.
there is no mongodb.conf file in the directory /etc/
instead, I located a mongodb.conf under usr/local/etc/ but the file is empty!?
I don't get it, what is wrong? MongoDB installation routine did not prompt any errors, hence I was expecting a fully operational mongodb.conf under /etc/
How am I supposed to change the MongoDB configuration now? Simply add the desired entries to the existing file under usr/local/etc/?
The correct location is /usr/local/etc/mongodb.conf, and the file is supposed to be empty. All you need is to add something like dbpath=/usr/data/db (or wherever you have created the directory to host your database) to be able to start the server.

Moving postgres data folder on Ubuntu

I have a web application querying a Postgresql database (successfully) and I'm looking to move the data folder from location /var/lib/postgres/9.3/main to a customisable location.
Right now I'm prevented from even copying the folder due to permission errors, but I can't assign myself the permissions because that breaks the postgres server.
(I broke the server by running sudo chown <username> -R /var/lib/postgres/9.3/main - which worked as a command but stopped the postgres server from working)
I would simply create a new folder and change the location there, but I'll lose the current instance of my database if that was done.
How can I move the current folder to a new location, so that I can point to it in the .conf file? I need to explicitly move the folder, I can't create a new DB.
You can just copy or move the directory, including all subdirs and files
cp -rp or mv should be enough for this.
Postgres must not be running while you are messing with the files
The base of the data-drectory (PG_DATA) must be owned by postgres and have file mode 0700 . (when not: pg will refuse to start)
[the rest of the files must at least be readable/writeble by postgres]
the new location must also be known to the startup process (in /etc/init.d/ and (possibly) in the postgres.conf file within the data directory. (for the log file location)