How to connect to postgres with two terminals? - postgresql

I have a terminal I can connect to postgres
psql testdb
but when I open a new tab or a new terminal and tried to connect to psql an error is returned
This error returns
psql: 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.5433"?
I tried creating another user and using another port
createuser testing_multiple
psql -p 5433
the same error occurs.

Your issue is probably that you're working within virtualenv. It isn't a chroot, but it sets a bunch of environment variables to fake the local environment, quite likely including the PATH and other settings.
Most likely the unix socket is at a different location to that compiled into the psql binary you're using. I'd say your PostgreSQL server socket is probably actually at /tmp/.s.PGSQL.5433, in which case export PGHOST=/tmp/ will work. Since you can connect from within the virtualenv terminal you can check though - within psql, run SHOW unix_socket_directories;. The location shown there is what you can give in PGHOST to connect to that PostgreSQL server.
The reason that export PGHOST=localhost works is that you are forcing psql (and other clients that use libpq) to connect over TCP/IP, instead of the default unix socket connection.

Related

How to open a postgres database created using psql in PgAdmin4?

How do I open a postgres database created using psql (i.e. in the terminal) in PgAdmin4 and vice versa?
I also noticed that the localhost port for psql is 5432 and for PgAdmin4 is 5433.
Is this correct?
Some background is in order. When you install Postgres you create an instance of a server that comes with three databases already created; template0, template1 and postgres. On a given machine you can create more then one Postgres server/instance and have them run simultaneously. In order for that to happen though each server needs to listen on a different port. The default port is the 5432 you mention above. It would seem you also have another server running on port 5433. I'm guessing what you want to know is about connecting to a given server rather then a database in that server. In that case it is important to know that Postgres works on the server/client model where it is the server and in your case psql and pgAdmin are the clients. What this means is that a Postgres server is not tied to a client, it exists on its own. It also means a client can connect to any Postgres server it can reach, assuming it is using the correct credentials. All of the previous means, yes you can connect psql and/or pgAdmin to either server. For psql specify the correct port using -p. For pgAdmin you will need to set up a server using the server dialog Dialog. Then use the appropriate port in the connection tab.

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.

Cannot connect PostgreSQL with psql - wrong UNIX-Domain-Socket

I'm running CentOS 7.5 and cannot setup PostgreSQL.
If I'm logged in as user postgres and type psql postgres I get the following error message:
psql: Could not connect to server: no such file or directory
does the server run locally and accepts connections on Unix-Domain-Socket "/var/run/postgresql/.s.PGSQL.5432"
However, I changed the port to 5543 (did so in etc/systemd/system/postgresql.service by including /lib/systemd/system/postgresql.service as a [Service] and setting Environment=PGPORT=5543). Note that you shouldn't change it directly in /lib/ because that will get overwritten.
So, the server looks for the wrong UNIX-Domain-Socket and does not find one (because it does not exist), but the socket for the correct port 5543 does exist according to sudo netstat -nlp:
5486/postgres /var/run/postgresql/.s.PGSQL.5543
postgresql.service is running according to systemctl status postgresql.service
Any constructive help is appreciated.
Why are you changing it in systemd and not using PostgreSQL's config file? Is that a CentOS thing?
Anyway - you can run the server on any port you like, or run multiple server instances of the same or different versions on a variety of ports. In that case though you need to tell psql what port to use.
You can set an environment variable (PGPORT), specify it with -p on the command-line or in a .psqlrc file. See the manuals for details.
Edit in response to comments:
If you want to set the PGPORT for psql, do it in the user's shell defaults or in /etc/bash... or equivalent. You could of course replace psql with an alias using your custom port or recompile the binary itself if you wanted.
I'm not sure this is really much use from a security perspective. It seems unlikely that someone can run local processes on your machine, has gained access to your postgres user password but isn't smart enough to see what port the server is running on.

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.

GPDB-psql: could not connect to server: No such file or directory

We have the small array of Greenplum database.In that, We have a master node. when I am trying to use PSQL utility
Getting this error :
[gpadmin#master gpseg-1]$ psql
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"?
We tried
on searching for postmaster.pid files.We have removed it.But still, the error remains.
You need to export your PGPORT and source Postgres.sh_path(inside your GPDB).
After that you will be able to do psql and connect to the database.