Postgresql not running? - postgresql

I installed PostgreSQL and trying to connect:
$ psql
psql: 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"?
$ ps aux | grep postg
$ sudo service postgresql start - does not result.
$ psql -U ivan -h localhost msg_proxy
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?
Why? My iptables seems ok:
$ sudo iptables-save
# Generated by iptables-save v1.6.0 on Mon Feb 6 13:31:17 2017
*filter
:INPUT ACCEPT [43867:5867960]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [34820:18177695]
COMMIT
# Completed on Mon Feb 6 13:31:17 2017
Output of sudo netstat -pant | grep postgres is empty

Related

No response from postgres server

I'm trying to set up a small postgresql server on my raspberry pi. So far, I've:
Installed postgres onto my pi
Set a static IP adress for my pi
edited /etc/postgresql/13/main/postgresql.conf so that listen_addresses = '*'
edited /etc/postgresql/13/main/pg_hba.conf to include
host all all 0.0.0.0/0 md5
Running pg_isready on my pi gives me
root#pibox:~# pg_isready
/run/postgresql:5432 - accepting connections
However, running the following results in no response
root#pibox:~# pg_isready -d <db_name> -h<static_ip> -p 5432 -U postgres
<pi's IP>:5432 - no response
Additionally, I'm not able to connect from my laptop
psql --host=<static IP> --port=5432 --dbname <db name> --username=postgres
psql: error: connection to server at "<the pi's IP>", port 5432 failed: Connection refused
What are some of the next steps I can take so that I can connect to my postgres server from my laptop and execute queries against the databases?
EDIT:
My laptop is running macOS 12.4. I'm connecting to my pi via ssh over my home network. The pi is running a copy of DietPi. Here is some more relevant info
root#pibox:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
Running the following commands yeilds these outputs
root#pibox:~# pg_isready
/run/postgresql:5432 - accepting connections
root#pibox:~# pg_isready -d postgres -h <Pi IP> -p 5432 -U postgres
<Pi IP>:5432 - no response
root#pibox:~# pg_isready -d postgres -h 127.0.0.1 -p 5432 -U postgres
127.0.0.1:5432 - no response
root#pibox:~# pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
13 main 5432 online postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log

Docker: Is the server running on host "localhost" (127.0.0.1) [duplicate]

This question already has answers here:
How do I access postgresql within Docker with sqlalchemy?
(3 answers)
Closed 3 months ago.
I'm trying to create a docker container for my fastAPI app. However whenever I try to run following compose file I get this error:
Error
docker logs api-api-1
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
When I first saw the error I thought that I run the database on the localhost of the container but I checked it and database is running on 0.0.0.0:5432.
docker logs api-postgres-1
2022-11-22 05:08:23.971 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
Dockerfile
FROM python:3.10
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Compose.YAML
version: '3'
services:
api:
image: fastapilearning
depends_on:
- postgres
ports:
- 80:8000
environment:
- ACCESSTOKENEXPIREMINUTE=${ACCESSTOKENEXPIREMINUTE}
- ALGORITHM=${ALGORITHM}
- DATABASEHOSTNAME=${DATABASEHOSTNAME}
- DATABASENAME=${DATABASENAME}
- DATABASEPASSWORD=${DATABASEPASSWORD}
- DATABASEPORT=${DATABASEPORT}
- DATABASEUSERNAME=${DATABASEUSERNAME}
- SECRETKEY=${SECRETKEY}
command: /bin/bash -c "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000"
postgres:
image: postgres
environment:
- POSTGRES_PASSWORD=${DATABASEPASSWORD}
- POSTGRES_DB=${DATABASENAME}
volumes:
- C:\Users\booruledie\Documents\Git\freecodecamp\dockerPostgresData\:/var/lib/postgresql/data
What's the issue here ?
I'm sure of the source code because when I try to run it on my local machine I didn't get any error.
Most likely PostgreSQL server didn't shut down correctly.
Try:
postgres -D /usr/local/var/postgres
You see:
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 449) running in data directory "/usr/local/var/postgres"?
Then Kill the port:
kill -9 PID

Connection to remote postgresql host running docker refused

I have an instance of postgresql running in a docker container.
I can connect to the database from the host that is running docker by:
docker exec -u root -it postgres bash
And then accessing the database from there by doing an su to user postgres.
If I use a client from a desktop pc / laptop to try and connect I get a connection refused:
psql -h 20.XXX.1XX.1XX -p 5432 -U <user>
psql: could not connect to server: Connection refused
Is the server running on host "20.XXX.1XX.1XX" and accepting
TCP/IP connections on port 5432?
I have edited the pg_hba.conf file in the docker instance and added the following:
host all all 0.0.0.0/0 md5
host all all ::/0 md5
If I run netstat, again within the container I get:
root#ee9dg39913cdc:/# netstat -na | grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
tcp6 0 0 :::5432 :::* LISTEN
unix 2 [ ACC ] STREAM LISTENING 52651 /var/run/postgresql/.s.PGSQL.5432
And when I run it on the machine hosting the docker instance:
root#VM01:~# netstat -na | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
I do not have ufw running at all, so, no firewall issues. The host is an Azure VM and port 5432 is open to the internet.
postgresql.conf is set as:
listen_addresses = '*'
Given all of the above, can anyone help me understand why I cannot connect to the postgres instance over the internet using:
psql -h 20.XXX.1XX.1XX -p 5432 -U <user>
Thanks.

access postgres into docker container when postgres locally is also running

I have postgres on docker container that I run using the following command:
docker run -d --name timescaledb -p 127.0.0.1:5433:5433 -e POSTGRES_PASSWORD=somepass timescale/timescaledb:latest-pg12
Meanwhile I also have postgres installed locally in my machine and port 5432 is listening:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 124 postgres 7u IPv6 0x38eef62419af50a3 0t0 TCP *:5432 (LISTEN)
postgres 124 postgres 8u IPv4 0x38eef624199e6d5b 0t0 TCP *:5432 (LISTEN)
com.docke 3520 alex 39u IPv4 0x38eef62428c6b7fb 0t0 TCP localhost:5433 (LISTEN)
com.docke 3520 alex 42u IPv4 0x38eef62428c3581b 0t0 TCP *:49816 (LISTEN)
I'm trying to set up a connection with psycopg2 using this command:
connection = psycopg2.connect(host=config.DB_HOST,database=config.DB_NAME,user=config.DB_USER,password=config.DB_PASS,port=config.DB_PORT)
of which config.xxx is :
DB_HOST = 'localhost'
DB_USER = 'postgres'
DB_PASS = 'somepass'
DB_NAME = 'etfdb'
DB_PORT = '5433'
but I'm getting the following error:
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5433 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5433 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
It's worth noting that the command docker ps shows me port 5432 for some reason I don't know:
❯ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d2623bd4055 timescale/timescaledb:latest-pg12 "docker-entrypoint.s…" 6 days ago Up 17 minutes 5432/tcp, 127.0.0.1:5433->5433/tcp timescaledb

Postgres in Docker port matching

I need to run several equals Postgres containers on different ports via docker-compose. My problem is that I do not understand how to connect from my terminal to container's psql.
First of all, I should say that my local Postgres is off, so 5432 is free. Also I have listen_addresses = '*' in my containers.
Here is list of my attempts (to make it easier, I've posted examples for one Postgres container):
Run without port specifying
docker-compose.yml:
version: "3"
services:
postgres:
build:
context: ../../
dockerfile: db-load-test/sharding-benchmark/Dockerfile
environment:
- POSTGRES_PASSWORD=postgres
check port:
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------
sharding-benchmark_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp
try to connect:
$ psql -p 5432 -U postgres
psql: 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"?
$ psql -h localhost -p 5432 -U postgres
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?
$ psql -h 0.0.0.0 -p 5432 -U postgres
psql: could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
Specify port and expose it in .yaml (I've changed 5432 to 5433 to make it more expressive)
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------------------------------------
sharding-benchmark_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp, 0.0.0.0:49162->5433/tcp,:::49162->5433/tcp
Similar attempts to connect was unsuccessful. Also it's unclear to my why Postgres still listen 5432:
postgres_1 | 2021-08-02 12:49:01.394 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-08-02 12:49:01.394 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-08-02 12:49:01.397 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Use docker directly
docker run --rm -it -e POSTGRES_PASSWORD=postgres -p 5432:5432/tcp postgres:11
It works with psql -h localhost -p 5432 -U postgres, but if I use docker run with 5432:5433 it breaks, which is unclear to me.
In your docker-compose.yml file, you need to specify ports: for each of the database containers. In each of these the second number must be the normal port for the database (5432); the first number can be any number of your choosing that's not used by another container or host process.
version: '3'
services:
pg1:
image: postgres
# Use default PostgreSQL port 5432 on the host
ports: ['5432:5432']
pg2:
image: postgres
# First port must be different from pg1's above
# Second port must be exactly 5432
ports: ['5433:5432']
When you connect from the host, use a host name of localhost and the first published ports: number. (If you're using the older Docker Toolbox setup, use the docker-machine ip address of the Docker VM; if you're connecting from a different host, use the Docker host's DNS name or IP address.)
# connect to pg1
psql -h localhost -p 5432 -U postgres
# connect to pg2
psql -h localhost -p 5433 -U postgres
You do not need to expose: ports in the docker-compose.yml file; this doesn't really actually do anything. You shouldn't need to reconfigure the stock postgres image beyond its basic environment-variable settings. It doesn't make sense to connect to 0.0.0.0, a special address that means "everywhere" when you're setting up a network listener.
If you're connecting from a different container, none of this matters. Use the Compose service name of the other container (like pg2) and the standard port number (5432). ports: are completely ignored, and localhost means "this container". The two containers must be on the same Docker network; Compose will create a network named default and just using that without explicit networks: settings is fine.