PostgreSQL 9.6 won't connect on Ubuntu 16 - postgresql

I installed PostgreSQL 9.6 on my Ubuntu 16.04 system. It seems like it's up and running, but when I try to make a connection it just fails. When I run the status command I see that it's always saying exited for some reason. I see lots of things in a google search but none seem to be helping.
I changed the first entry in the pg_hba.conf to be local all postgres trust
% ps augxw | grep postg
postgres 769 0.0 0.3 303964 24384 ? S 22:13 0:00 /usr/lib/postgresql/9.6/bin/postgres -D /var/lib/postgresql/9.6/main -c config_file=/etc/postgresql/9.6/main/postgresql.conf
postgres 772 0.0 0.0 303964 3956 ? Ss 22:13 0:00 postgres: 9.6/main: checkpointer process
postgres 773 0.0 0.0 303964 3956 ? Ss 22:13 0:00 postgres: 9.6/main: writer process
postgres 774 0.0 0.0 303964 3956 ? Ss 22:13 0:00 postgres: 9.6/main: wal writer process
postgres 775 0.0 0.0 304408 6572 ? Ss 22:13 0:00 postgres: 9.6/main: autovacuum launcher process
postgres 776 0.0 0.0 158964 3204 ? Ss 22:13 0:00 postgres: 9.6/main: stats collector process
ubuntu 1492 0.0 0.0 12944 936 pts/0 S+ 22:14 0:00 grep postg
%
% sudo su - postgres
$ psql -h localhost
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
$ exit
logout
% systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2017-03-30 22:13:57 PDT; 1min 19s ago
Process: 901 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 901 (code=exited, status=0/SUCCESS)
Tasks: 0
Memory: 0B
CPU: 0
CGroup: /system.slice/postgresql.service
Mar 30 22:13:57 ip-172-31-9-223 systemd[1]: Starting PostgreSQL RDBMS...
Mar 30 22:13:57 ip-172-31-9-223 systemd[1]: Started PostgreSQL RDBMS.

You are connecting using TCP/IP socket, but local refers to unix-domain socket.
The first field is the connection type: "local" is a Unix-domain socket, "host" is either a plain or SSL-encrypted TCP/IP socket, "hostssl" is an SSL-encrypted TCP/IP socket, and "hostnossl" is a plain TCP/IP socket.
I did install postgresql-9.6 from that repo and:
host all postgres 127.0.0.1/32 trust does what you want to do
postgres#lkaminski-ubuntu-desk:~$ psql works out of the box, no need to change config. It is using unix-domain socket that is already trusted. So you can just drop -h localhost and no need to edit configs.
$ sudo grep -e "^[^#]" /etc/postgresql/9.6/main/pg_hba.conf
local all postgres peer
host all postgres 127.0.0.1/32 trust
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5

The issue ended up being that version 9.5 had the normal 5432 port and so when 9.6 got installed/started, it pointed to 5433. Deleting the 9.5 install and editing the config file to point at 5432 fixed the issue.

Related

Unable to connect to server: FATAL: Ident authentication failed for user "postgres"

I got message: Unable to connect to server: FATAL: Ident authentication failed for user "postgres" when I tried to create a connection in pgAdmin.
pgAdmin parameters:
-------------------
Hostname/address: localhost
Port: 5432
Maintenance database: postgres
Username: postgres
Password: postgres
The package is install on a Linux machine. I tried web mode and desktop mode, the problem appeared on both.
Below are the setup steps I follow after installing the package:
# vi /opt/pgdata/pg_hba.conf
host all all all md5
# vi /opt/pgdata/postgresql.conf
listen_addresses = '*'
port = 5432
# postgresql-setup --initdb
* Initializing database in '/var/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
# cp /opt/pgdata/pg_hba.conf /var/lib/pgsql/pg_hba.conf
# cp /opt/pgdata/postgresql.conf /var/lib/pgsql/postgresql.conf
# systemctl enable postgresql
# systemctl start postgresql
# su - postgres
$ psql postgres
psql (10.6)
Type "help" for help.
postgres=# \password postgres
Enter new password: postgres
Enter it again: postgres
postgres=# \q
# systemctl stop postgresql
# systemctl start postgresql
# systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: active (running) (thawing) since Tue 2022-07-26 21:23:49 EDT; 28min ago
Process: 40022 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
Main PID: 40025 (postmaster)
Tasks: 8 (limit: 72897)
Memory: 19.3M
CGroup: /system.slice/postgresql.service
├─40025 /usr/bin/postmaster -D /var/lib/pgsql/data
├─40026 postgres: logger process
├─40028 postgres: checkpointer process
├─40029 postgres: writer process
├─40030 postgres: wal writer process
├─40031 postgres: autovacuum launcher process
├─40032 postgres: stats collector process
└─40033 postgres: bgworker: logical replication launcher
Jul 26 21:23:49 localhost.localdomain systemd[1]: Starting PostgreSQL database server...
Jul 26 21:23:49 localhost.localdomain postmaster[40025]: 2022-07-26 21:23:49.641 EDT [40025] LOG: listening on IPv6 address "::1", port 5432
Jul 26 21:23:49 localhost.localdomain postmaster[40025]: 2022-07-26 21:23:49.641 EDT [40025] LOG: listening on IPv4 address "127.0.0.1", port 5432
Jul 26 21:23:49 localhost.localdomain postmaster[40025]: 2022-07-26 21:23:49.642 EDT [40025] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Jul 26 21:23:49 localhost.localdomain postmaster[40025]: 2022-07-26 21:23:49.645 EDT [40025] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
I can see the status is "active (running) (thawing) since Tue 2022-07-26 21:23:49 EDT; 28min ago"
is that normal?
How to connect to the instance successfully?
Try this
$ sudo psql -U postgres
postgres=# \l
postgres=# \dt
or may be you forget to restart service daemon after change conf
$ sudo systemctl daemon-reload
hopefully this heplful
There's a copy of pg_hba.conf under:
/var/lib/pgsql/data
which is the actual version used by the instance. Put the configuration in this file resolved the issue

Unable to create database - psql: FATAL: role "dyadav" does not exist - Mac OS

I installed postgres using brew.
DYadav-MBP-2:~ dyadav$ brew install postgresql#9.6
Below is my .bash_profile file.
DYadav-MBP-2:~ dyadav$ cat .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export PATH=/usr/local/opt/postgresql#9.6/bin/:$PATH
I have also added 127.0.0.1 postgres to /etc/hosts.
Started postgres using below's command:
brew services start postgresql#9.6
But when I run psql postgres, I get "psql: FATAL: role "dyadav" does not exist"
I following many solutions provided in stack overflow, nothing really helped.
fyi: postgres is running on user dyadav.
DYadav-MBP-2:~ dyadav$ ps aux | grep postgres
dyadav 2338 0.0 0.0 4340528 896 ?? Ss 8:12AM 0:00.05 postgres: stats collector process
dyadav 2336 0.0 0.0 4485592 1968 ?? Ss 8:12AM 0:00.04 postgres: autovacuum launcher process
dyadav 2335 0.0 0.0 4485528 1000 ?? Ss 8:12AM 0:00.05 postgres: wal writer process
dyadav 2334 0.0 0.0 4485528 2020 ?? Ss 8:12AM 0:00.08 postgres: writer process
dyadav 2332 0.0 0.0 4485528 1072 ?? Ss 8:12AM 0:00.00 postgres: checkpointer process
dyadav 2302 0.0 0.1 4485808 17252 ?? S 8:12AM 0:00.06 /usr/local/opt/postgresql#9.6/bin/postgres -D /usr/local/var/postgresql#9.6
dyadav 4641 0.0 0.0 4268056 804 s000 S+ 9:41AM 0:00.00 grep postgres
psql assumes you want to connect to the database as the current operating system user. So psql postgres tries to connect to a database named postgres using the database user dyadav.
As you want to create a database you need to connect as the superuser postgres (a database user, not an operating system user):
psql -d postgres -U postgres
-d specifies the database to connect to, -U specifies the database user to use.
Then you can run the create database command to create another database.

postgres PEM server, issue connecting the data base

I'm not good with Postgres database troubleshooting, but I have a big project data base running on it.
Today suddenly I'm getting an error while I try to access my server, attached is the picture of the error screenshot. I really don't want to lose my data because its not backed up for few months.
I tried to reinstall but it says the port is messed up somewhere.
The message while I reinstall is:
"psql:could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "127.0.0.1" and accepting
TCP/IP connection on port 5432?
Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432? That are two questions you could answer:
Is the server running on host "127.0.0.1" (aka localhost) use:
ps auxw | grep postgres # to answer this question
... accepting TCP/IP connections on port 5432? there are several ways to answer this question:
psql -h 127.0.0.1 -p 5432 postgres
telnet localhost 5432
nmap -p 5432 localhost
The result for (1) should be like:
postgres 30402 0.0 0.1 238488 12924 ? S 12:09 0:01 /opt/postgres/bin/postmaster -D /data/db/postgres/pgdata
postgres 30409 0.0 0.0 94644 1308 ? Ss 12:09 0:00 postgres: logger process
postgres 30411 0.0 0.3 238628 46964 ? Ss 12:09 0:01 postgres: checkpointer process
postgres 30412 0.0 0.0 238628 4764 ? Ss 12:09 0:00 postgres: writer process
postgres 30413 0.0 0.0 238628 5832 ? Ss 12:09 0:00 postgres: wal writer process
postgres 30414 0.0 0.0 239036 2820 ? Ss 12:09 0:01 postgres: autovacuum launcher process
postgres 30415 0.0 0.0 97884 2460 ? Ss 12:09 0:03 postgres: stats collector process
The result for (2) should be like:
Password:
The result for (3) should be like:
Trying ::1...
Connected to localhost.localdomain.
Escape character is '^]'.
The result for (4) should be like:
Starting Nmap 6.40 ( http://nmap.org ) at 2016-05-18 15:14 CEST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000050s latency).
Other addresses for localhost (not scanned): 127.0.0.1
rDNS record for 127.0.0.1: localhost.localdomain
PORT STATE SERVICE
5432/tcp open postgresql
Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds
if (1) fails (gives no relevant output) postgres is obviously not running.
you didn't start it (there should be a file for the service in /etc/init.d/)
you attempted to start it, but it failed to startup
the binaries were not present, not found, or wrong
the data directory was not present, not found, or had the wrong owner/rights
the data directory contained files, but not for this version
if (2,3,4) fail (give no relevant output)
postgres is not listening on the inet socket, but only on the unix-domain socket : check your config-file for listen_address = '*'
finally, there could be a firewall-issue. (Check your iptables settings. or whatever)

How to start postgreSQL9.4 server installed by homebrew

I install postgresql9.4.5 by homebrew.
Before 9.4.5, I used 9.3.9 by macport, but I want to use only homebrew.
So I uninstall 9.3.9 and macport and install 9.4.5 by homebrew.
I could success "initdb /usr/local/var/postgres".
But, when I enter "postgres -D /usr/local/var/postgres", show error.
The error
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.
WARNING: could not create listen socket for "localhost"
FATAL: could not create any TCP/IP sockets
I tyied to start server by manual
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
But, pg_ctl -D /usr/local/var/postgres status show pg_ctl: no server running.
I confirmed pg_hba.conf and it shows
# IPv4 local connections:
host all all 127.0.0.1/32 trust
Also, when I enter psql, shows the error psql: FATAL: could not open relation mapping file "global/pg_filenode.map": No such file or directory
What do I need to do? Please tell me.
Add a postscript
I also tried ps aux | grep postgres
postgres 276 0.0 0.0 2470236 452 ?? Ss 火12AM 0:03.18 postgres: stats collector process
postgres 275 0.0 0.0 2614684 1160 ?? Ss 火12AM 0:11.63 postgres: autovacuum launcher process
postgres 274 0.0 0.0 2614552 416 ?? Ss 火12AM 0:00.79 postgres: wal writer process
postgres 273 0.0 0.0 2614552 440 ?? Ss 火12AM 0:00.81 postgres: writer process
postgres 272 0.0 0.0 2614552 596 ?? Ss 火12AM 0:00.07 postgres: checkpointer process
postgres 242 0.0 0.0 2614552 1184 ?? S 火12AM 0:01.07 /opt/local/lib/postgresql93/bin/postgres -D /opt/local/var/db/postgresql93/defaultdb
root 75 0.0 0.0 2469228 884 ?? Ss 火12AM 0:00.03 /opt/local/bin/daemondo --label=postgresql93-server --start-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql93-server/postgresql93-server.wrapper start ; --stop-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql93-server/postgresql93-server.wrapper stop ; --restart-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql93-server/postgresql93-server.wrapper restart ; --pid=none
This shows some process is active. And, 2 lines show macport pass. But, I had deleted /opt/local folder.
I killed these process and ps aux | grep postgres did not show anything.
So, I tried postgres -D /usr/local/var/postgres. Next time, this shows
LOG: database system was shut down at 2015-11-19 18:45:38 JST
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
And, the terminal stopped and did not show prompt. So I had to enter 'control + c'
But, I could start postgres manually. I did this command pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
And I could do psql commands.

How to install PostgreSQL 9.1 on OSX Lion

I downloaded PostgreSQL from the official website and ran the .dmg installer. After that I downloaded pgadmin3 and I am indeed able to connect to the database.
when I run 'psql' I get the following error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
After hours of googling I read about some $PATH issues, so I put this into my .bashrc:
export PATH=/Library/PostgreSQL/9.1/bin:$PATH
However, this doesn't solve the error above at all. After some more hours of googling I tried to run 'psql -l localhost -U postgres'. This gives another error:
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (fe80::1) and accepting
TCP/IP connections on port 5432?
After some more googling I tried to edit /Library/PostgreSQL/9.1/data/pg_hba.conf and replaced all occurrences of 'md5' with 'trust'.
Then I changed user to postgres and executed 'pg_ctl stop' and 'pg_ctl start', switched back to my own user and tried to connect again, no luck.
Here are some more infos:
[~]$ which psql
/Library/PostgreSQL/9.1/bin/psql
ps aux | grep postgres
postgres 19022 0.0 0.0 2446096 484 ?? Ss 11:31PM 0:00.01 postgres: stats collector process
postgres 19021 0.0 0.0 2486532 1776 ?? Ss 11:31PM 0:00.01 postgres: autovacuum launcher process
postgres 19020 0.0 0.0 2486400 576 ?? Ss 11:31PM 0:00.03 postgres: wal writer process
postgres 19019 0.0 0.0 2486400 820 ?? Ss 11:31PM 0:00.05 postgres: writer process
postgres 19017 0.0 0.0 2446096 356 ?? Ss 11:31PM 0:00.01 postgres: logger process
postgres 19015 0.0 0.1 2486400 8216 s001 S 11:31PM 0:00.17 /Library/PostgreSQL/9.1/bin/postgres
sudo find / -name .s.PGSQL.5432
No file was found?!?
Update 1:
In /etc/sysctl.conf I added the values suggested by the installer's README:
kern.sysv.shmmax=1610612736
kern.sysv.shmall=393216
kern.sysv.shmmin=1
kern.sysv.shmmni=32
kern.sysv.shmseg=8
kern.maxprocperuid=512
Before these settings, the installer quits with an error, afterwards the wizard appears and installs postgres (and again, using pgadmin3 works, so I assume that the database is running fine).
Run lsof on the master process to all of this. In your case it's 19015 (shown with my PID):
> sudo lsof -p 286 | awk '$5 == "unix" && $NF ~ /\// { print $NF }'
/tmp/.s.PGSQL.5432
You can leave off the awk, but basically it's getting the UNIX socket on which postgres is listening. From there, you can use the -h option to psql (but only include the directory).
> psql -h /tmp template1
template1=# \q
If that doesn't work, you can check the lsof output to show you what TCP port it's actually listening on, and if it's not 5432, use the -p option to postgres
If you are able to connect to the database through pgadmin3, then your database is running.
(1) I'd verify by running netstat -an | grep 5432 on the command line, as a paranoia check. You are in business i.e. you actually installed Postgresql if you get this below:
tcp4 0 0 *.5432 . LISTEN
tcp6 0 0 *.5432 . LISTEN
ffffff80133bfed8 stream 0 0 ffffff8013be8000 0 0 0 /tmp/.s.PGSQL.5432
(2) Speaking for myself, I found that just running psql gets me your psql error message. I had to run
psql -h 127.0.0.1 -p 5432 [-d database] -U postgres
to connect with the database. In other words, I had to explicitly specify IP and port. Since you were able to run psql and get a non-connect error message, the OS understands that psql is a command and you don't have a pathing issue.