Installing Software, How to Find Unix Socket - postgresql

I'm in the process of installing a program called Moodle onto our server. During the installation process it asked for the Database port and Unix socket for the PostgreSQL database.
I found the database port with a sql command but I don't know how to find the Unix socket.
I'm vaguely familiar with the concept from my Unix classes but I don't know too much about this.
Please help!

The location for the unix domain socket will be configured in the postgresl.conf file.
eg:
unix_socket_directories = '/run/postgresql'
The name of the socket will be the same as the TCP port number, same config file.
eg:
port = 5432
So, here the socket is at /run/postgresql/5432

Related

How to connect to PostgreSQL?

I want to connect to a PostgreSQL server with rust-postgres:
let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
The complete code example is from Client and Config.
I keep getting the error
Error: Error { kind: Connect, cause: Some(Os { code: 111, kind: ConnectionRefused, message: "Connection refused" }) }
In a terminal, I can interact with PostgreSQL:
(base) wm#wm:~/Desktop/HP$ sudo -i -u postgres
postgres#wm:~$ psql
psql (10.10 (Ubuntu 10.10-0ubuntu0.18.04.1))
Type "help" for help.
postgres=#
There are few issues relevant to failed connections, so I guess there must be something I missed.
Postgresql supports client connections via local Unix sockets or via TCP/IP connections.
In the default configuration, though, it will not listen for TCP/IP connections. It will only listen for connections to a local Unix socket. The location of this socket is defined by the postgresql configuration variable unix_socket_directories.
In your tests, you have found that when running the psql command line tool with no arguments, it manages to connect to the database. This works because psql uses the postgresql supplied client library (libpq) and this client library has the default behavior of connecting to the local Unix socket if no hostname is supplied.
However, when using rust-postgres, you are supplying a connection string including the text "host=localhost". This is instructing rust-postgres to make a connection to IP address localhost. It fails because the postgresql server is not configured to listen on any IP interfaces, and only listen on a Unix socket.
You could change the connection string to specify the unix socket to connect to, for example:
host=/var/run/postgresql/.s.PGSQL.5432
The value above reflects where the socket is on my machine, it might be different on yours.
The libpq library allows you to leave the host parameter out altogether and it will connect to the Unix socket automatically; unfortunately it does not look like rust-postgres supports this though.
Alternatively, you could reconfigure your postgresql server so that it listens on the loopback IP adapter. This involves setting the listen_addresses parameter in the postgresl configuration file. See this answer for more details, including changes you will need to make to the authentication in pg_hba.conf.

Perl DBI: connect to REMOTE PostgreSQL db still complains about local socket file

I'm trying to use DBIx::Class to connect to a remote PostgreSQL db.
My connect string: "DBI:Pg:database=asterisk:host=example.com:hostaddr=10.10.10.10:port=5432", $user, $pass
example.com does resolve to '10.10.10.10', swapping both to the IP or having only host=IP all result in
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"? at /usr/local/share/perl/5.10.1/DBIx/Class/Storage/DBI.pm line 1489. at db-access.pl line 26
On this debian I don't have or want postgresql, I do have libdbd-pg-perl however. And DBI->available_drivers says that I do have Pg (It didn't before I installed the library).
How can I find out what it's trying to do and do I need anything else installing? The server in question is configured to allow connections from 10.100.* addresses which the machine running this code is on and I can connect to it with tools such as PSequel and pgAdmin on my workstation (same network) with the db user in question.
"DBI:Pg:database=asterisk:host=example.com:hostaddr=10.10.10.10:port=5432"
That should be:
"DBI:Pg:database=asterisk;host=example.com;hostaddr=10.10.10.10;port=5432"
Only the first two separators are colons. The rest of them should be semi-colons.

Serving my postgres database online

I want to have a postgres database on a computer that I can use from multiple (external) computers. It will act as a trial server for me, leaving it on whenever I need it.
I researched how to do it and found out I had to forward the service postgres to the internet. Postgres is on port 5432. I logged in my router which has a forwarding option. I opened up the port 5432, but cant add postgres to the list of services.
Is there a reason for that?
Actually. I found that I just have to adapt the pg_hba.conf file (just started trying). I am running windows. Any advise is welcome, this is not my expertise. I dont understand why it would work if I just adapt the pg_hba.conf. For games or other services, like a game, I have to open a port in the router. Or should I do both?
From Postgres documentation - Client authentication is controlled by a configuration file, which traditionally is named pg_hba.conf and is stored in the database cluster's data directory. (HBA stands for host-based authentication.)
Each record specifies a connection type, a client IP address range (if relevant for the connection type), a database name, a user name, and the authentication method to be used for connections matching these parameters.
So it is absolutely required to set up your pg_hba.conf for it to allow access to other computers. You will also need to setup router and firewall settings for allowing incoming connections to port 5432.
Here is what you need to do
on postgres.conf change listen_address to:
listen_addresses = '*'
and on pg_hba add this to the end of the file
host all all 0.0.0.0/0 md5
And also make sure the port is forwarded to the machine running Postgres from your router

How to use Navicat to connect to Postgresql socket via SSH?

While the SSH tunnel functionality in Navicat seems to work well if postgresql is running on a TCP port, it seems impossible for me to make it work when postgresql is accessible using a file socket?!
Is this true, or I am missing something?
Correct. There's no sensible way to connect to a unix socket over a ssh forward. You could possibly use socat to link a forwarded tcp/ip port to a unix socket, but I wouldn't bet on it working.
Just use TCP/IP.

pgAdmin and PostgreSQL: can't connect to server

I just installed PostgreSQL on Snow Leopard and can't connect to the database server via pgAdmin 3.
I'm on my local machine, however I keep getting this error:
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 5423?
I'm a bit of a noob when it comes to Postgres, so I'm not really sure what the problem is.
I can, however, log in through the command line, via psql -U postgres, and start and stop the server successfully.
Any help would be much appreciated.
The error message pointed out that you tried to connect to server on port 5423. However, postgres server listens on 5432 by default.
From your above comment (SHOW port; gives me "5432"), I think you need to change the port to 5432!
UPDATE: Tuan Dang spotted it. I'll leave this answer in place in case it helps someone else for whom the issue isn't quite the same.
Since you can connect via the command line, run:
SHOW port;
from psql. You'll probably see that the port is not 5432. You need to connect to the port PostgreSQL is actually running on from your application.
It's also possible that it just isn't listening on TCP/IP. Run:
SHOW listen_addresses;
to see what it's listening for.
The reason you can connect via the command line is likely to be because the command line psql you're using is connecting over a unix socket (since you didn't specify a host) and your app is connecting via tcp/ip.