Mounting pgsql volume in Docker - postgresql

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

Related

How can I change my postgresql WAL file size?

How can I change my PostgreSQL WAL file size as my wish. By default at pg_wal directory postgres generate 16MB WAL files. I try to change using max_wal_size, min_wal_size parameter. But I think it is not right parameter to change. Necessary details given in the below screenshot.
With an old and outdated version like 9.4, your only option is to build PostgreSQL from source and configure it with
./configure --with-wal-segsize=1024
Then you have to dump and restore the cluster to that new installation.
With current versions. You could simply shut down PostgreSQL cleanly (important!) and run pg_resetwal with the appropriate option.
Upgrade!

PostgreSQL data directory configuration

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.

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.

Is it possible to reinstall Postresql without destroying existing databases?

I have a Postgresql 10 installation on Ubuntu 18.04 that somehow broke and won't restart. Can I just reinstall it without destroying its databases, so that I can access the databases again? pg_dump doesn't work.
Yes, you can do that.
By default, your databases and other important files are stored in PGDATA.
Traditionally, the configuration and data files used by a database cluster are stored together within the cluster's data directory, commonly referred to as PGDATA (after the name of the environment variable that can be used to define it). A common location for PGDATA is /var/lib/pgsql/data.
https://www.postgresql.org/docs/10/storage-file-layout.html
I don't know how you will uninstall PostgreSQL, but be sure to keep PGDATA.
(yum or apt won't delete PGDATA)
After re-installing PostgreSQL, make sure to launch your PostgreSQL with your existing PGDATA
pg_ctl start -D YOUR_EXISTING_PGDATA/

Backup and restore postgresql data folder directly

Till now I've been backing up my postgresql data using pg_dump, which exports the data to an sql file mydb.sql, and then restoring from that sql file using psql -U user -d db < mydb.sql.
For one reason or another it would be more convenient to restore the database content more directly, in an environment where psql does not exist... specifically on a host server where postgresql is installed in a docker container running on the host, but not on the host itself.
My plan is to back up the content of /var/lib/postgresql/data/ to a tar file, and when required (e.g. when a new server is created that hosts the postgresql container) just restore that to the same path. The folder /var/lib/postgresql/data/ in the docker container is mapped to a folder on the host server, so I would create this backup on the host, not inside the postgres container.
Is this a valid approach? Any "gotchas"? And are there any subfolders within /var/lib/postgresql/data/ that I can exclude from the tar file? I don't want to back up mere 'housekeeping' information.
You can do that, but you have to do it properly if you don't want your database to become corrupted.
Either stop PostgreSQL before copying the data directory or follow the instructions from the documentation for an online backup.