Permanently change permissions of /var/run/postgresql
Solution 1
Permissions for /var/run/postgresql are taken from /usr/lib/tmpfiles.d/postgresql.conf
Solution 2
Run the following to reclaim the directory (immediately and on subsequent boots):
service='reclaim-postgresql'
service_file="/etc/init.d/${service}"
echo "#!/bin/sh
set -e
### BEGIN INIT INFO
# Provides: ${service}
# Required-Start: \$postgres-xc
# Required-Stop: \$postgres-xc
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Reclaim /var/run/postgresql
### END INIT INFO
chown -R ${USER}:${USER} /var/run/postgresql" \
| sudo tee "${service_file}"
sudo chmod +x "${service_file}"
sudo update-rc.d "${service}" defaults
sudo service "${service}" start
Note that when running sudo apt-get upgrade
to update postgres, you may need to temporarily sudo chown -R postgres:postgres /var/run/postgresql
in order for the upgrade to succeed.
Related videos on Youtube
![Jackson](https://i.stack.imgur.com/LO71w.jpg?s=256&g=1)
Jackson
Professional software developer and life-long computer enthusiast. Has JavaScript flowing out of his ears. Bash and LISP are also fun. Live free or die GNU/Linux. Graphical and typographical obsessor.
Updated on September 18, 2022Comments
-
Jackson almost 2 years
On Linux Mint 17.0 Cinnamon Edition, I want to use
/var/run/postgresql
as theunix_socket_directories
option for all of my postgres databases.Whenever I run the command
pg_ctl -D postgres-data -o '-F -p 33311' start
to start my local database, I get the errorFATAL: could not create lock file "/var/run/postgresql/.s.PGSQL.33311.lock": Permission denied
.As many answers across AskUbuntu, StackOverflow and various forums suggest, I have to run
sudo chmod 777 /var/run/postgresql
orsudo chown -R $USER:$USER /var/run/postgresql
to change the permissions of the directory so that I can write to it. Then I am able to start my database. However, every time I reboot, the permissions go away; the directory is no longer world-writable and the userpostgres-xc
reclaims user and group ownership over the directory.Neither
sudo adduser $USER postgres
orsudo adduser $USER postgres-xc
alleviates the permissions issue.I tried doing a clean install as described here, but I still have the issue on reboot.
I've tried changing the value of
unix_socket_directories
to/tmp
in./postgres-data/postgresql.conf
(the local database config file), and this allows me to start that particular database without having to update permissions on every reboot. However, I also have a script which usespg_ctl -D $DIR initdb && pg_ctl -D $DIR start
to setup and start databases on the fly. As such, a defaultpostgresql.conf
config file is used for these databases, sounix_socket_directories
is the unwritable/var/run/postgresql
and I get permission denied errors when running that script. Manually/programmatically editing these configs on-the-fly to useunix_socket_directories = '/tmp'
is... undesirable.I know that I could use the
-o
flag withpg_ctl
to override theunix_socket_directories
option to/tmp
, but this script is shared by other developers who do not use Ubuntu, so I would not necessarily want to restrict everyone to using the/tmp
directory, especially if their configurations differed slightly. I would prefer everyone use the default location for their installation. Personally, I would also prefer to keep the default directory for general compatibility with other Ubuntu packages; e.g., pgadmin looks at/var/run/postgresql
for a lock file by default.I have also tried editing
/etc/postgresql/9.3/main/pg_ctl.conf
in an attempt to automatically pass set these options whenever I usepg_ctl
:# Automatic pg_ctl configuration # This configuration file contains cluster specific options to be passed to # pg_ctl(1). pg_ctl_options = '-o "-c unix_socket_directories=/tmp -c unix_socket_group=jackson -c unix_socket_permissions=0777"'
But that did not have any effect.
Please advise on how I can use
/var/run/postgresql
as myunix_socket_directories
option for all of my postgres databases without having to runsudo chown -R $USER:$USER /var/run/postgresql
every time I restart my system. Thanks.-
user1174838 about 9 yearsWhat kind of file system is sitting in? When you reboot/restart the system, does this directory exist? Just wondering if this is sitting in a tmpfs file system or similar.
-
user1174838 about 9 yearsAfter a reboot, does the directory exist?
-
Jackson about 9 yearsYes, the directory exists.
-
Craig Ringer about 9 yearsX-post from askubuntu.com/q/631112/75715
-
Jackson about 9 yearsI flagged the other post for takedown, Craig.
-
-
Sylvain over 3 yearsThis should be the accepted answer.
-
cazort almost 3 yearsAt what point do these permissions get set from the file? I ran chmod on the file and it worked...restarted the server and nothing changed. Is it created on boot or under some other circumstance?