How do I get a Postgres server to createdb and shut down? - postgresql

I have an instance of a Postgres server running that I've started with the command:
pg_ctl -D /usr/local/var/postgres/data -l /usr/local/var/postgres/data/server.log start
Running the command createdb test prompts me for my password twice, then I get this error:
createdb: could not connect to database template1: FATAL: password authentication failed for user "joey"
Also, when I try to stop the server using
pg_ctl -D /usr/local/var/postgres/data stop -m smart
I get this error message:
pg_ctl: PID file "/usr/local/var/postgres/data/postmaster.pid" does not exist
Is server running?
Is there something I'm missing or forgot to initialize/install? I used these instructions to install.
I checked this answer and this answer and neither of the two fixed my problem.

From the log file you added in your comment, it looks as if either there is another postgresql instance running on the machine (or possibly something else which is using the same port as postgresql wants to use):
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
This is stopping the database from starting up to begin with.
To see what process that could be, you can use lsof:
$ sudo lsof | grep -i listen
...
postmaste 19732 postgres 3u IPv6 1194355 0t0 TCP localhost:postgres (LISTEN)
postmaste 19732 postgres 4u IPv4 1194356 0t0 TCP localhost:postgres (LISTEN)
...
In the above sample, from a Linux host, you can see that a process called postmaster (it is truncated in the printout) listens on localhost:postgres, meaning the localhost address port 5432 (lsof is translating the port 5432 into 'postgres' via the file /etc/services which contains a mapping between well known port numbers and corresponding services).
The fact that createdb is prompting for your password implies that it is connecting to a database somewhere, although I could not spot it the ps printout you sent.
The other part of your question was why createdb could not connect to your database (or whatever database is running on your machine). If it is a freshly created database cluster then it will not have any users defined other than the default 'postgres' user. You must issue commands with this user:
createdb -U postgres test
Without the -U option it will try to connect using your current login user, which won't exist in the database.
It might also be that you will need to authenticate as the postgres user. The file pg_hba.conf in the postgresql data directory controls what kind of authentication will be needed.
In general the postgresql documentation is excellent; I suggest you read through the section Server Setup and Operation to check that you have a valid configuration.

Related

pgpool-II Session to Watchdog Delegate Ip Terminated When Either Primary or Standby Node Failed

I'm trying to setup postgres cluster of two nodes (primary and standby). In order to activate automatic failover, I'm using pgpool-II.
I followed the following article: https://www.pgpool.net/docs/41/en/html/example-cluster.html
and the only thing difference I did is installing postgresql version 12 instead of version 11.
Knowing that I'm trying it useing two centos7 images on VMware. I faced the following issues:
When I run systemctl status pgpool.service on both nodes, it returned success.
Also I can access postgresql using the watchdog delegate IP.
But what testing failover, everything goes wrong.
Scenario 1:
I accessed my database using watchdog delegate Ip.
I disconnect the standby server.
Result: My session to postgresql continued to work for less than a minute and then it failed. and I'm unable to connect again, until I reconnect the standby node, and restart the pgpool service again.
Scenario 2:
I accessed my database using watchdog delegate Ip.
I disconnect the primary server.
Result: My session stopped directly. and the standby server is not promoted to be master.
I noticed something (might be related to the above described problem): when I try to run the following command
psql 192.168.220.146 -p 9999 -U postgres -c "show pool_nodes"
it fails to work and returned the following:
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.9999"
However if I ran: psql 192.168.220.160 -p 5432 -U postgres
it works fine and I can access the postgres shell.
My pool_hba file:
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all pgpool 0.0.0.0/0 scram-sha-256
host all postgres 0.0.0.0/0 scram-sha-256
Any help would be appreciated.
I followed the following article: https://www.pgpool.net/docs/41/en/html/example-cluster.html and the only thing difference I did is installing postgresql version 11.
I not ping delegate_IP = '192.168.1.233'. May i help you?
Thanks you.
you are not providing -h argument to psql for specifying the IP address. So effectively psql is trying to connect to UNIX domain socket and considering the IP address in the command as the database name.
Try putting -h before the IP address
psql -h 192.168.220.146 -p 9999 -U postgres -c "show pool_nodes"

Heroku Postgresql pg:psql No Route to host

For now I am just trying to make the heroku pg:psql command work but my final purpose is to copy a database that I have on my computer (localhost) to the heroku postgresql database with the pg:push command.
For now when I simply try to access the database that I created on heroku, the heroku pg:psql command returns:
psql: could not connect to server: No route to host
Is the server running on host "ec*-**-***-***-**.eu-west-1.compute.amazonaws.com" (**.***.***.**) and accepting
TCP/IP connections on port 5432?
postgresql.conf: (the lines are not commented)
listen_address ='*'
port = 5432
ssl = true
and host all all **.***.***.** trust in pg_hba.conf
I also tried to add rules to iptables in order to give access to the database from the host IP address provided by heroku.
I am on a Debian computer, how can I solve this?
psql: could not connect to server: No route to host
It means your PostgreSQL server is not starting up or is starting up on a different port.
Solutions you may try:
Check PostgreSQL service by command ps -ef | grep Postgres.
Check the port which PostgreSQL is listening to by command netstat -tupln | grep Postgres.
Make sure your server enables UDP port because PostgreSQL needs UDP port loopback for stats collector service.
Check the startup logs or database logs at pg_log about the problem.

psql cant connect to PostgreSQL server (postmaster) on IP and port 5432?

Please read before replying it as duplicate (as it perhaps can happen). I am running my postmaster (postgres) server. See below for 'sudo netstat -anp|grep 5432' output?
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 29606/postmaster
unix 2 [ ACC ] STREAM LISTENING 1650581 29606/postmaster /var/run/postgresql/.s.PGSQL.5432
unix 2 [ ACC ] STREAM LISTENING 1650582 29606/postmaster /tmp/.s.PGSQL.5432
I am able to connect from localhost using
psql -h localhost (OR 127.0.0.1) -d <DB> -U user -W
But when I try to connect from other hosts using tcp, by specifying
psql -h ip_add_postmaster -d <DB> -U user -W
It throws:
psql: could not connect to server: Connection refused
Is the server running on host XXXXXX and accepting TCP/IP connections on port 5432?
What's wrong here?
pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
In postgresql.conf,
listen_addresses = 'localhost, 127.0.0.1, ip_add_postmaster'
Note: ip_add_postmaster is same as my Elastic IP and not public DNS. If this information
matters.
What am I doing wrong here? Machine is hosted on Amazon EC2 and have open the port 5432.
As your netstat output indicates, it's listening at 127.0.0.1:5432 which is localhost. That is only connectable from localhost ;)
Set listen_addresses='*' in your config and it will work.
[edit]
Other things to check:
is the amazon firewall blocking anything?
is iptables blocking anything?
But first make sure the listening address is correct, your netstat output shows that it won't work like this.
listen_addresses='localhost, private_ip' fixed the issue. I was not able to start postmaster server on elastic IPs. Once postgres server started o localhost and private IPs, I was able to connect.
One other issue I have found was if you end up with two Postgres installations, the second one can choose non-default port (in my case it was 5433 i/o 5432). So checking the port in postgresql.conf might be a good idea.
I ran into this issue and tried all sorts of fixes I found across SO, and want to add a simple solution that worked for me after realizing it had to do with permissions in my case.
Simply, if you're running a psql server on Windows, you are initially restricted to the default postgres superuser for logging in, launching the server, and so on.
So, first try running from the command line:
psql -U postgres -h localhost -p 5432
and enter your password at the prompt. If you've managed to login and the server is up, then it was a permissions issues. From here, you can create a role for yourself that has login privileges to whatever database you are trying to run.
If the error persists, then consider checking postgresql.conf as mentioned above, to make sure default IP is set to * or localhost, and the port set to 5432 or whatever port you want as default.
I also ran into the same issue. On debugging, it was nothing related to the port, but due to some missing directories in the Postgres folder.
While updating Mac OS (from 10.13.1 -> 10.13.13), some folders in the directory /usr/local/var/postgres/ gets deleted. The fix was the adding the missing directories:
mkdir /usr/local/var/postgres/pg_tblspc
mkdir /usr/local/var/postgres/pg_twophase
mkdir /usr/local/var/postgres/pg_stat
mkdir /usr/local/var/postgres/pg_stat_tmp
mkdir /usr/local/var/postgres/pg_replslot
mkdir /usr/local/var/postgres/pg_snapshots
mkdir /usr/local/var/postgres/pg_logical/{snapshots,mappings}

Postgres Database Local Installation / Running

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.

Cannot connect to postgres from remote host

I have a database server (192.168.1.50) running postgres. I have created a database named "testdb" and a user "testuser" with password "testuserpw".
Locally, I can connect to the db using:
psql -d testdb -U testuser
When I issue the command from another host (192.168.1.60):
psql -h 192.168.1.50 -d testdb -U testuser
I have the error:
psql: could not connect to server: Connection refused
Is the server running on host "192.168.1.50" and accepting
TCP/IP connections on port 5432?
Any idea ?
Check the setting of listen_addresses in your postgresql.conf file. Many distributions make it default to 127.0.0.1, i.e. listen only to connections coming in from localhost. It should be set to '*' to listen for connections on all interfaces.
If you are still having trouble, use lsof to see what network sockets the postgres process is listening on.
On Ubuntu, I noticed that remote access at some point stopped working (currently using 9.1.9). The reason is, that postgres is no longer started with the -i switch [1] so no matter what you configure for listen_addresses, it will be ignored.
Fortunately, adding the following line to /etc/environment solves the problem after logging out and in again (or reboot):
PGOPTIONS="-i"
See [2] for more options. Note, that adding this to /etc/postgresql/9.1/main/environment did NOT work for me.
Now, when doing nmap ip-of-my-remote-server I finally get this again:
5432/tcp open postgresql
Yay!
[1] http://www.postgresql.org/docs/9.1/static/runtime-config-short.html
[2] http://www.postgresql.org/docs/9.1/static/libpq-envars.html
Is the firewall letting the connections through? Or, check if pg_hba.conf allows connecting from addresses other than localhost.
The listen_address configvar in postgresql.conf is not the only way to get postgres to listen on the non-local IP-address (or addresses).
Use option "-o -h *" if you start postgres from pg_ctl, otherwise do add "-h" "*" to the postgres command line, like e.g.
/usr/local/pgsql/bin/postgres -D /pg/data "-h" "*"
Of course /pg/data must be changed to your current datapath.
This is especially useful when experimenting.
I came across the same problem as yours, and my source of problem is the firewall settings.
If you're using Ubuntu, print your firewall status:
sudo ufw status verbose
It may looks like this:
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
...
The default rule of incoming connection is "deny", so you need to specify the "allow"ed port.
type:
sudo ufw allow 5432/tcp
reference:
https://www.vultr.com/docs/how-to-configure-ufw-firewall-on-ubuntu-14-04
Connection refused (0x0000274D/10061) i fixed here with:
Open the terminal and type:
VIM /var/lib/pgsql/data/postgresql.conf
Edit the "listen_adresses", it should be set to '*'
After this, rest it on terminal:
/scripts/restartsrv_postgres