I decided to get some experience using postgresql this week and had some trouble getting it to start on my machine. What follows is a brief explanation of how to get it working for myself and anyone like me.
when postgresql is installed it creates a postgres user that you will use when setting up the database server. postgresql also creates multiple commands. You will be using initdb
, createuser
and pg-ctl
you will also want to use sudo -i -u postgres
or an equivalent in order ot execute commands as the postgres user.
Following a normal tutorial you will create a database in /var/lib/postgres/data
as the postgres user. The init command will then inform you that you can start the server using pg_ctl -D /var/lib/postgres/data -l logfile start
(note: you will have to be in the postgres user still, since you're normal account doesn't own the files) once you run that command, the server will instantly quit and when you check the logs it will tell you something like FATAL: could not create lock file "/run/postgresql/.s.PGSQL.5432.lock": No such file or directory
.
Now, when I see something like that, I'm prone to thinking that there must be some elegant solution or some flag I forgot to set that I have to go read a tutorial for. After a few hours it doesn't seem to be the case (though I think this issue might be patched in future releases actually based on a github discussion I saw). Instead you should manually create the file and make sure it's owned by the postgres user and group permissions are also set to postgres.
Once that's done you can start the server and now you can create a user for your normal account (make sure it matches) so if my user is called "foo" I will run createuser --interactive foo
and be sure to set it as a super user unless I want to have to escalate permissions again when creating databses etc.
I messed that up the first time and had to remove the user by doing dropuser foo
and re create it with superuser permissions.