How to solve psql error: could not connect to server? - postgresql

I'm trying to install GeoDjango for my website. To do that, I first need to create a PostgreSQL database. Following this tutorial, I'm trying to connect to PostgreSQL by using sudo su - postgres and then psql. The problem is that I get this error : 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.5432"?
I made some researchs and found this post explaining how to solve the problem. However, when I run sudo service postgresql start I have to type the password for postgres but I don't know what it is, I never created a password for postgres. I've made researchs about that and I can't find a solution because all solutions need to first connect to psql in order to alter the existing user. But I need first to find the password in order to solve the problem of connection to psql.
What am I supposed to do ?
Thanks in advance !
PS : I'm using windows 10 with the Ubuntu terminal.
PS2 : I've found some topics saying it's easier to install PostgreSQL and PostGIS using Docker. However, there are lots of compatibility problems with Docker due to the fact I'm using windows 10. Do you think I should use Docker ?

I've found the solution !
So for those interested, I had to change the password in order to get log in and that solve the psql error: could not connect to server. To change the password, simply type sudo passwd postgres and set up a new password. I hope this will help other people !

Related

Brew psql asks for password to user that I haven't created

I am trying to install postgresql on my MacBook using brew. I ran the command
brew install postgres
and when the download is finished I start the service using
brew services restart postgresql
This works fine, the problem arrises when I either want to log in to psql by running psql
in the command line or when I want to create a database using
createdb
My problem is that psql asks me for a password to my user which I haven't created. The username is the same as the one for my computer so my first thought was that the password would be the same but alas it was not.
Here is the error I get after answering the password incorrectly a couple of times
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: password authentication failed for user "<MyUsername>"
I have switched out my actual username for <MyUsername> but you get the point
I have scoured the web for answers to this. Here are the things that I have tried:
The password to my computer (several hundreds of times, just to be sure)
The default password postgres (supposedly just postgres)
The empty password
Logging into the postgres (psql -U postgres) user and trying the same passwords.
Going into the pg_hba.conf file to change the database administrative login
Uninstalling and Reinstalling a couple of times
Bashing my head against the wall (really mad this one didn't work)
Hopefully I'm being completely blind and the default password is something obvious
Apparently the bane of my postgres cli was the pgAdmin tool which had accompanied the installation of postgres from postgresql.org. Deleting pgAdmin and following this article to completely uninstall postgres and then reinstalling psql via brew install postgres solved my issue

Can't connect to postgres via PSQL or php, only pgAdmin4

I have PostgreSQL 12 installed and using Windows 10. My goal is to connect to postgre via php, because it didn't work I tried with plsql but it didn't work either, i always get (with both plsql and php using PDO):
FATAL: password authentication failed for user "postgres"
Connection via plsql in cmd
However, when trying via pgAdmin4, I can connect and access my databases. I first use my root password then sometime my postgres user one, so I'm sure my password works.
Here is my pg_hba.conf file:
pg_hba.conf file
I have a set password for postgres which was set vua pgAdmin4.
I don't understand why it would work one way and not the other, can anyone help me?
So after trying several things, I found that plsql and php error was due to the wrong port being adressed.
By default 5432 is used, but for some reason my configuration was using 5433.
You can find this information in pgAdmin4 by opening PostgreSQL 12 properties, and then checking in connection tab.
To change the used port in plsql I used:
psql -U postgres -p 5433
Change 5433 by your configured port if you have same issue, you also need to specify the port if using php's PDO.

pqxx: broken_connection on trying to connect to posgres db

I have this problem trying to connect to the local postrgres databases. Until now i succeeded to connect to the same databases using jdbc or working directly in PGAdmin, this error happens when i try to connect using pqxx library or from shell:
pqxx code:
#include <pqxx/connection.hxx>
int main(){
pqxx::connection conn("user=postgres port=5432 dbname=pqxx-test password=postgres");
return 0;
}
or from shell, as user postgres:
psql pqxx-test
both the cases raise this error:
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.5432"
Searching for the solution i think i understood i have to edit pq_hba.conf and postgresql.conf files in order to connect in these ways, but in pg_hba.conf is already set:
local all all md5
and in postgresql.conf
listen_addresses='*'
And by the way, i'm not even able to edit these files because of a problem with the viminfo, but maybe this is another problem..
Can anyone help me?
edit:
using PostgreSQL 9.6.2
on Ubuntu 14.04
edit:
as postgres user, if i ask for the postgre version using:
psql -V
the result is 9.6.2
but looking at the file system hierarchy, all conf file are inside this folder
/opt/PostreSQL/9.4/data/
can that be the problem?
You seem to have a mix of client-side postgresql 9.6 configured for the ubuntu FS layout and a postgresql server 9.4 presumably downloaded from EntrepriseDB (and installed under /opt/PostgreSQL) using its own layout.
Assuming that the postgresql is running, the simplest solution is to add host=localhost to your connection string, so that it will use a TCP connection instead of the Unix domain socket /var/run/postgresql/.s.PGSQL.5432 that does not exist because of the mismatch.

pgAdmin III : No servers showing up

I've followed many online tutorials on how to install pgAdmin correctly, I've installed and uninstalled it multiple times but whenever install it. It shows Server Groups without any servers in it.
Then when I try and register a new server using localhost for both name and host I get this error:
server doesn't listen
Searched for days trying to find a solution for this, any ideas?
Happened with me as well, when I started pgadmin3 for the first time, I was expecting it to ask for the password which I provided during but it didnt ask and then didnt display any servers.
Anyways, you can create a server yourself.
If your server is on the same machine,
provide a name: test
provide server: localhost
provide a port number : 5432 (default port)
provider a username: postgres (default user, you can change later in login roles)
password: provide something
Restart if required, and it will then show you a
database with postgres (default db),
tablespace, and login roles under your server, and you should be good to go .
First Create a Server with the following credential-
Host Name/address : localhost
Port : 5432
Maintenance database : postgres
Username : postgres
Password : *Your_Password*
Note : Before Creating the Server first start the postgresql server
Reinstalled program and saved it to directory C:\ instead of C:\program_Files
When I did this invalid password message appeared. So I had to change postgres user password:
In the terminal window:
Switch to the postgres account on the server by typing:
sudo -i -u postgres
Now I can access a Postgres prompt immediately by:
psql
Change the postgres user password:
ALTER USER postgres WITH PASSWORD 'newpass';

Openbravo - Connecting pgadmin with postgresql database in Ubuntu

I have installed Openbravo ERP 3.0 in Ubuntu 12.04(LTS). After completed the installation I tried to connect with postgresql database using pgadmin 9.1. I gave all the details as mentioned in this link: http://wiki.openbravo.com/wiki/Installation/Appliance/Openbravo.
Also I have changed the config file settings like listen_addresses="*". I got this error after I click "Ok" button
Could anyone give suggestion to fix this error? Thanks in advance.
Actually the problem comes because of not mentioning the local cluster or data area for PostgreSQL. To do this after installing Openbravo, need to give this command in terminal
psql -d openbravo -U tad -h localhost -p 5932
Finally give the PostgreSQL configuration details as mentioned in this link
It works fine now...
To be able to reach the server remotely you have to add the following line into the file: /var/lib/pgsql/data/postgresql.conf:
listen_addresses = '*'
PostgreSQL, by default, refuses all connections it receives from any remote address. You have to relax these rules by adding this line to /var/lib/pgsql/data/pg_hba.conf:
host all all 0.0.0.0/0 md5
This is an access control rule that lets anyone login from any address if a valid password is provided (the md5 keyword). You can use your network/mask instead of 0.0.0.0/0 to only allow access from certain IP addresses.
When you have applied these modifications to your configuration files, you will need to restart the PostgreSQL server.
/etc/init.d/postgresql start
Now, you will be able to login to your server remotely