Postgres Database Local Installation / Running - postgresql

Trying to setup Postgres / PostGIS on OSX Mountain Lion to use with Django. However, something is wrong and I am not sure how to fix it.
$ createdb geo
createdb: could not connect to database postgres: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
$ postgres -D /usr/local/var/postgres
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 221) running in data directory "/usr/local/var/postgres"?
$ pg_ctl -D /usr/local/var/postgres -l logfile start
pg_ctl: another server might be running; trying to start server anyway
server starting
Then I found this gist that said how to fix it on lion. However it did not work for me:
$ kill -9 221
$ pg_ctl -D /usr/local/var/postgres stop
waiting for server to shut down.... done
server stopped
$ pg_ctl -D /usr/local/var/postgres start
pg_ctl: another server might be running; trying to start server anyway
server starting
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 1436) running in data directory "/usr/local/var/postgres"?
Another attempt:
$ cd /usr/local/var/postgres
$ mv postmaster.pid postmaster.backup
$ pg_ctl -D /usr/local/var/postgres start
server starting
$ LOG: could not bind IPv6 socket: Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG: could not bind IPv4 socket: Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG: could not bind IPv6 socket: Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
WARNING: could not create listen socket for "localhost"
FATAL: could not create any TCP/IP sockets
Has anyone successfully done this with mountain lion? If so, what am I missing? Thanks for your help with this issue!

This message:
createdb: could not connect to database postgres: could not connect to
server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
indicates that your PostgreSQL server doesn't use /var/pgsql_socket as its Unix domain socket directory, which is the rendez-vous point between the server and the client. This is quite common, since only PostgreSQL shipped by Apple with OSX server uses that path. Incidentally, this is why the createdb command as shipped by Apple tries to connect to the server as configured by Apple.
From that point, there are a lot of different solutions to work around the problem, but the more obvious one would be to use the createdb command that comes with your PostgreSQL installation instead of the one that is preinstalled in your system.
Should you need further help with that, you should specify how you installed PostgreSQL, because there are quite a few different packages that provide it, and they all use different paths to avoid trampling over each other.
Update for Homebrew
If using brew, I suggest to apply the answer on superuser.com:
How should I set the PATH variable on my Mac so the Hombrew-installed tools are found?
Presumably the author of the Postgres+Postgis tutorial you're referring to has changed its PATH long ago and forgot to mention it in his text.
Once done, the command which createdb should return /usr/local/bin/psql which is the homebrew version instead of the initial /usr/bin/psql which is the Apple version.

Related

Postgresql not starting even after editing rc file

I receive the following error when I try to start postgresql aka psql in Slackware Linux:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
I am trying to start it and changed the field in pg_hba.conf to trust from md5.
At some point it was asking for a password.
I get the following when I try to start the server:
bash-5.1# postgres -D /pgsql/data
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server.
bash-5.1# postgres -D /pgsql/data >logfile 2>&1 &
[1] 8310

Installing PostgreSQL and `createdb` with Terminal

After installing PostgreSQL from the terminal with Homebrew...
➜ ~ brew link postgresql
Warning: Already linked: /usr/local/Cellar/postgresql/11.2_1: 3,186 files, 35.3MB
To relink: brew unlink postgresql && brew link postgresql
➜ ~ brew services restart postgresql
Successfully stopped postgresql (label: homebrew.mxcl.postgresql)
Successfully started postgresql (label: homebrew.mxcl.postgresql)
➜ ~ createdb 'test'
createdb: could not connect to database template1: could not connect to server: No server file or directory
Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
I would like to be able to run strictly from terminal and not be subject to using the PSequel GUI...
Thanks,
Solved
My main issue was:
➜ ~ createdb 'test'
createdb: could not connect to database template1: could not connect to server: No server file or directory
Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
this issue was simply solved by:
➜ ~ postgres -D /usr/local/var/postgres
which invocated the database directory...
2019-05-06 14:20:37.367 EDT [41854] LOG: listening on IPv6 address "::1", port 5432
2019-05-06 14:20:37.367 EDT [41854] LOG: listening on IPv4 address "127.0.0.1", port 5432
2019-05-06 14:20:37.367 EDT [41854] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-05-06 14:20:37.384 EDT [41854] LOG: database system is ready to accept connections
keeping that running, and opening a new-tab on my terminal:
➜ ~ createdb 'test'
➜ ~ psql 'test'
psql (11.2)
Type "help" for help.
test=#
Something else of use during my debugging:
remove old database files (this is dangerous)
➜ ~ rm -rf /usr/local/var/postgres
followed-up with
➜ ~ initdb /usr/local/var/postgres
initdb is not used to create a "new database"
As Documented in the Manual you need it to create a "cluster" or "data directory" which then stores databases created with createdb
Quote from the manual:
Before you can do anything, you must initialize a database storage area on disk. We call this a database cluster. (The SQL standard uses the term catalog cluster.) A database cluster is a collection of databases that is managed by a single instance of a running database server
[...]
In file system terms, a database cluster is a single directory under which all data will be stored. We call this the data directory or data area
In short: initdb creates the necessary directory layout on the harddisk to be able to create and manage databases.
It's a necessary part of the installation process of a Postgres server.

Postgres/Mac - 'service already loaded', but 'could not connect to server'

I'm running Homebrew-installed Postgres version 9.6.3 on my Mac (High Sierra, 10.13.3), and this morning I'm finding that Postgres is having some issues. It was working fine last night, then I put the computer to sleep... when I woke it up this morning and tried to run a Phoenix app, I got
[error] Postgrex.Protocol (#PID<0.306.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
Running psql returned
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
so it seemed that perhaps the server had stopped... however, running my alias pg-start, which translates to launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist returns
/usr/local/Cellar/postgresql/9.6.3/homebrew.mxcl.postgresql.plist: service already loaded
So this is confusing, because it seems that one command suggests that Postgres is not running, while the other suggests that it is.
I can't recall for sure, but I may have stopped the server before putting the computer to sleep last night, which I actually usually do not do... my pg-stop alias is launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
though I can't see why that would cause problems, it's the only thing that sticks out in my mind as something 'different' that I may have done.
I've tried restarting my machine, but the problem persists. I'm not terribly experienced with debugging this sort of issue, so any guidance or suggestions would be much appreciated.
Well, I resolved it, though I'm not sure what the exact problem was. To get some more error info, I ran
postgres -D /usr/local/var/postgres
which gave me
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 323) running in data directory "/usr/local/var/postgres"?
which I'd encountered before, so I kind of knew how to proceed...
Here are the steps I took to resolve this:
First, I ran
pg_ctl -D /usr/local/var/postgres start
which returned
pg_ctl: another server might be running; trying to start server anyway
server starting
My-MBP:~ me$ FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 1188) running in data directory "/usr/local/var/postgres"?
then I ran my alias pg-stop
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
followed by
pg_ctl -D /usr/local/var/postgres start
again. This time, it returned
server starting
My-MBP:~ me$ LOG: database system was shut down at 2018-02-07 11:10:43 EST
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
and now Postgres was running correctly - psql commands, etc.
However, now my alias pg-stop wouldn't work -
/Users/me/Library/LaunchAgents/homebrew.mxcl.postgresql.plist: Could not find specified service
I ran
pg_ctl -D /usr/local/var/postgres stop
and then my pg-stop alias was restored. So now pg-start and pg-stop are working as they should be.
I hope that this is helpful to someone in the future, but if anyone can explain what happened here, I'd really appreciated having a deeper understanding of what went wrong.

Postgres service started, but can't connect. Nothing listening on port 5432

I don't know how this happened, but my postgres server (9.6.2) has stopped working locally. I seem to be able to start it up, and stop it using:
brew services start postgresql and brew services start postgresql but when I try to connect using psql I get this error:
psql: could not connect to server: No such file or directory Is the
server running locally and accepting connections on Unix domain
socket "/tmp/.s.PGSQL.5432"?
When I try to see if anything is listening on port 5432 using lsof -i :5432 I get nothing.
Looked at other posts that say check the pg_hba.conf file, which I did, but doesn't seem to be the problem.
I ended up reinstalling postgres via homebrew. That by itself didn't do enough. I had to kill my data directory to get this to work (i.e. rm -rf /path/to/my/data/directory) and then rebuild it using initdb.
This solution obviously only works if you don't care about keeping your data (in my case, it was just data for my dev environment, so I didn't). If you do want to save a copy of your data, I you'd have to come up with something else.

Postgresql starting failed

I was connected with remote postgres-9.3 by pgAdmin III. After close pgAdmin i try to connect on another day with the same db.
/etc/init.d/postgresql-9.3 status
dead but pid file exists
service postgresql-9.3 start FAIL
In pgstartup.log file i have:
This account is currently not available
pg_hba.conf:
# "local" is for Unix domain socket connections only
local all all peer
host all all 127.0.0.1/32 trust
host all all 89.70.224.82/32 md5
Operating system on server is CentOS. I don't know unfortunately how postgres was installed on the server because someone else done that.
What i can do with this?
The solution is reset your PostgreSQL logs
[root#user /]#/usr/pgsql-9.3/bin/pg_resetxlog -f /usr/pgsql-9.3/data/
after executing above command display "Transaction log reset"
and then restart PostgreSQL server
[root#user /]# /etc/init.d/postgresql-9.3 restart
Stopping postgresql-9.3 service: [FAILED]
Starting postgresql-9.3 service: [ OK ]
[root#qa /]# /etc/init.d/postgresql-9.3 status
(pid 3003) is running...
after checking your pgadmin
Try to start cluster using native postgres utility.
Assuming you have CentOS 6.6 and postgresql 9.4
sudo -u postgres /usr/pgsql-9.4/bin/pg_ctl -D <YOUR_DATA_DIR> start
You will get some reasonable output.
Then try to start postgres in single user mode
sudo -u postgres /usr/pgsql-9.4/bin/postgres --single -D <YOUR_DATA_DIR> -P -d 1
Beware that resetting xlogs will cause some data lost.
I also recommend to read this article http://blog.endpoint.com/2014/11/when-postgres-will-not-start.html