Ask Your Question

Moving postgresql 9.4 data directory in fedora 22

asked 2015-07-23 02:15:04 -0500

tyus gravatar image

I would like to know how to move the PostgreSQL database directory in a Fedora 22 installation. In Ubuntu, this is very straightforward by using pg_dropcluster and sudo pg_createcluster -d /the/new/location/ 9.4 main, but Fedora does not appear to have anything of the sort.

I copied the directory /var/lib/pgsql/data to a new location and, did the following:

su - postgres
service postgresql stop
export PGDATA="/the/new/location/"
echo $PGDATA # gives the correct /the/new/location/
service postgresql start
show data_directory;

Which still results in the default /var/lib/pgsql/data...

Could someone please either provide a link to a relevant and up-to-date tutorial or explain how to complete the move?

Note: I am aware that this question has been answered for older versions of Fedora and Psql, but it seems a lot of the files have been moved about and none of the approaches seem to work for me.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2016-06-05 06:16:13 -0500

adundovi gravatar image

More as an addition to javierwilson's answer, I believe the proper way would be to avoid modifying postgresql.service directly and introduce an extension of it [1]:

export PGHOME=/the/new/location/data
mkdir /etc/systemd/system/postgresql.service.d
cat <<'EOF' >> /etc/systemd/system/postgresql.service.d/pgdata.conf
# Location of database directory
systemctl daemon-reload

And to handle SELinux [2]:

semanage fcontext -a -t postgresql_db_t "$PGDATA(/.*)?"
restorecon -R -v $PGDATA

Finally, enable and start the service:

systemctl enable postgresql.service
systemctl start postgresql.service


edit flag offensive delete link more

answered 2015-07-28 11:05:16 -0500

javierwilson gravatar image

-D works with pg_ctl too, however, if you're using systemd, have a look at /usr/lib/systemd/system/postgresql.service there you have:

# Location of database directory

Just remember, SELINUX may complain if you change PGDATA.

If you do not want to touch systemd files, see the section "FILE LOCATIONS" in postgresql.conf. This would allow you to have the config files where -D or PGDATA say they should be but have the data somewhere else, because data_directory overrides -D and PGDATA for the location of the data directory


# The default values of these variables are driven from the -D command-line
# option or PGDATA environment variable, represented here as ConfigDir.

#data_directory = 'ConfigDir'           # use data in another directory
                                        # (change requires restart)
#hba_file = 'ConfigDir/pg_hba.conf'     # host-based authentication file
                                        # (change requires restart)
#ident_file = 'ConfigDir/pg_ident.conf' # ident configuration file
                                        # (change requires restart)
edit flag offensive delete link more

answered 2015-07-24 12:02:33 -0500

wavis gravatar image

Try exporting PGDATA in ~postgres/.bashrc (/var/lib/pgsql/.bashrc).

It would make sense that 'service' would operate without knowledge of locally defined environment variables.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-07-23 02:15:04 -0500

Seen: 2,511 times

Last updated: Jun 05 '16