How to connect cvat postgres db to Dbeaver - postgresql

Docker container for cvat_db has following settings:
services:
cvat_db:
container_name: cvat_db
image: postgres:10-alpine
restart: always
environment:
POSTGRES_USER: root
POSTGRES_DB: cvat
POSTGRES_HOST_AUTH_METHOD: trust
volumes:
cvat_db:/var/lib/postgresql/data
networks:
cvat
While below is the connection setting in dbeaver, where "HOST IP" i have put the IP address where cvat is hosted.
Dbeaver Settings
I'm getting error of timeout connection. So, I want to know how to connect postgres database to dbeaver.

Keep the following in mind:
Postgres always need a password according to their docs.
Do not create custom networks if it is not really needed. Use the default bridge network instead.
Do you connect with Postgres from another docker container or from your host system? If you connect from your host system add ports with 5432:5432.
mount your volumes to a subpath instead of named volumes
Example compose file:
version: '3.9'
services:
cvat_db:
container_name: cvat_db
image: postgres:10-alpine
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: cvat
volumes:
- ./cvat_db:/var/lib/postgresql/data
I wrote an article about docker compose networking, perhaps it helps.

Related

Authentication failed when logging in to Postgres database created via docker-compose

I have set up the following docker-compose.yml file to set up and run PostgreSQL and PgAdmin.
version: '3.1'
services:
db:
image: postgres:latest
container_name: postgres-dopp
restart: unless-stopped
environment:
POSTGRES_USER: dopp_dev
POSTGRES_PASSWORD: dopp_dev_pass
PGDATA: /data/postgres
ports:
- "5432:5432"
volumes:
- dbdata-dopp:/data/postgres
networks:
- network-dopp
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin4#pgadmin.org
PGADMIN_DEFAULT_PASSWORD: admin
PGADMIN_CONFIG_SERVER_MODE: 'False'
depends_on:
- db
volumes:
- dbdata-dopp:/data/pgadmin
ports:
- "5050:80"
networks:
- network-dopp
networks:
network-dopp:
driver: bridge
volumes:
dbdata-dopp:
name: dopp-db-data
driver: local
This works fine, insofar as I can navigate to PgAdmin in my host machine's browser and through that I can connect to the database using the credentials I've defined in the environment variables. However, when attempting to make a direct connection to the postgres database from my host machine (by connecting to localhost:5432, since I have configured to expose that port), I then get the following error response:
[28P01] FATAL: password authentication failed for user "dopp_dev"
I'm fairly new to the peculiarities of Postgres and docker configuration, so I'm not sure what is causing Postgres to say that password authentication fails when connecting from my host machine, while it works perfectly fine if I do it through PgAdmin, which is on the same internal docker network.
Actually, I discovered that the docker postgres service's port 5432 was being shadowed by a local postgres instance running my host machine.

How to make postgres listen on the container's new exposed port (not 5432)?

I try to initialize a database with Go.
I use port 5433 at postgres:alpine because 5432 is already taken by another microservice app.
func Init() {
DB, err = gorm.Open(postgres.New(postgres.Config{
DSN: "host=url_db user=gorm password=gorm dbname=gorm port=5433 sslmode=disable TimeZone=Asia/Tokyo",
}), &gorm.Config{})
if err != nil {
panic(err)
}
autoMigration()
}
url_db:
build:
context: ./api/services/url/db
dockerfile: Dockerfile
container_name: "url_db"
environment:
POSTGRES_USER: gorm
POSTGRES_PASSWORD: gorm
POSTGRES_DB: gorm
POSTGRES_HOST: url_db
ports:
- 5433:5433
You can confirm that only 5432 is exposed here.
I tried to expose 5433 by creating a new Dockerfile like this.
FROM postgres:alpine
EXPOSE 5433
But I got this error.
failed to initialize database, got error failed to connect to `host=url_db user=gorm database=gorm`: dial error (dial tcp 172.19.0.3:5433: connect: connection refused)
This comment:
Simply exposing the port on the docker image won't do anything unless postgres is actually configured to listen on that port. – super 5 mins ago
that teaches me the title(How can I expose a new port(not 5432) at postgres:alpine image?) is not the point, so I updated the title.
How to make postgres listen on the container's new exposed port (not 5432)?
You have multiple options:
Option 1: Define own postgresql.conf
url_db:
build:
context: ./api/services/url/db
dockerfile: Dockerfile
container_name: "url_db"
command: postgres -c "config_file=/etc/postgresql/postgresql.conf"
environment:
POSTGRES_USER: gorm
POSTGRES_PASSWORD: gorm
POSTGRES_DB: gorm
POSTGRES_HOST: url_db
ports:
- 5433:5433
volumes:
- /path/to/config:/etc/postgresql/postgresql.conf
Postgres has an example config at /usr/share/postgresql/postgresql.conf.sample within the container.
To get the config run:
docker run -i --rm postgres cat /usr/share/postgresql/postgresql.conf.sample > my-postgres.conf
Option 1: Overwrite the RUN command
url_db:
build:
context: ./api/services/url/db
dockerfile: Dockerfile
container_name: "url_db"
command: postgres -c port=5433
environment:
POSTGRES_USER: gorm
POSTGRES_PASSWORD: gorm
POSTGRES_DB: gorm
POSTGRES_HOST: url_db
ports:
- 5433:5433
You can have multiple containers that are internally listening on the same port, so long as they're mapped to different ports on the host (if they're published at all). In your example, you can set
url_db:
image: postgres:latest
environment:
POSTGRES_USER: gorm
et: cetera
ports:
- 5433:5432
Connections from outside Docker reach the remapped port, on <host ip>:5433. Connections between Docker containers use the standard service port, on url_db:5432. These connections ignore (and don't require) ports:.
"Expose" in modern Docker means almost nothing; it is most valuable as documentation in an image showing what port(s) the service normally uses. You can in theory ask Compose to expose: additional ports without modifying the image, but there's no practical effect from doing so.
For Docker Compose assuming we want to change port to 5433
An alternative to doing this is to do the following in your docker-compose.yml file
you can mount the postgres data into a volume in your directory in this case ./db
postgres:
image: postgres:10.14-alpine
environment:
POSTGRES_DB: iam
ports:
- 5433:5433
volumes:
- ./db:/var/lib/postgresql/data
Find the postgresql.conf file, then search the port
change the port to
port = 5433 # (change requires restart)

How to restrict remote access to postgresql docker container?

Using docker-compose I've developed a website which includes a Postgresql database. I deployed it on a server using the same docker-compose.yml file, but I found that I can remotely access my postgres server using psql.
I currently have these lines in my docker-compose
version: '3.6'
services:
db:
image: postgres
ports:
- 5432:5432
environment:
POSTGRES_DB: my_website
POSTGRES_USER: my_website
POSTGRES_PASSWORD: my_password
Does anybody know how I can only allow access to the postgres db from within the docker network so that it cannot accessed from outside the host OS?
At the moment you are binding the port 5432 from within the docker container onto a host port using the ports directive. To only have the port accessible within your local docker network, change ports to expose
version: '3.6'
services:
db:
image: postgres
expose:
- "5432"
environment:
POSTGRES_DB: my_website
POSTGRES_USER: my_website
POSTGRES_PASSWORD: my_password

Where does the data of postgresql container save ans, and also where the location of container is?

I'm using the nameko from GitHub https://github.com/nameko/nameko-examples
and I deploy this application on docker. Once I stop the container all the data lost and I also not able to find the location of the container. I want to store each database date on my local computer.
You have to mount storage for the postgres container.
https://github.com/nameko/nameko-examples/blob/master/docker-compose.yml
postgres:
container_name: nameko-example-postgres
image: postgres
ports:
- "5433:5432" # Exposing Postgres on different port for convenience
environment:
POSTGRES_DB: "orders"
POSTGRES_PASSWORD: "password"
POSTGRES_USER: "postgres"
restart: always
you can add columes to the above snippet by
volumes:
- ./database:/var/lib/postgresql

Cannot connect to postico from docker-compose postgresql service

I've done a docker-compose up and been able to run my web service attached to a postgresql image. Problem is, I can't view the data on postico when I try to access the database. The name of the image is db and when i try to specify hostname to be "db" on postico before i connect, i get an error saying hostname not found. I've entered my credentials, port and database name the same way i keyed them in my docker-compose file.
Does anybody know how i can find the correct setup to connect to within the container?
version: '3.6'
services:
phoenix:
# tell docker-compose which Dockerfile it needs to build
build:
context: .
dockerfile: Dockerfile.phoenix.development
# map the port of phoenix to the local dev port
ports:
- 4000:4000
# mount the code folder inside the running container for easy development
volumes:
- ./my_app:/app
# make sure we start mongodb when we start this service
# links:
# - db
depends_on:
- db
- redis
environment:
GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID}
GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET}
FACEBOOK_CLIENT_ID: ${FACEBOOK_CLIENT_ID}
FACEBOOK_CLIENT_SECRET: ${FACEBOOK_CLIENT_SECRET}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: ${POSTGRES_USER}
go:
build:
context: .
dockerfile: Dockerfile.go.development
ports:
- 8080:8080
volumes:
- ./genesys-api:/go/src/github.com/sc4224/genesys-api
depends_on:
- db
- redis
- phoenix
db:
container_name: db
image: postgres:latest
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: ${POSTGRES_USER}
volumes:
- ./data/db:/data/db
restart: always
redis:
container_name: redis
image: redis:latest
ports:
- "6379:6379"
volumes:
- ./data/redis:/data/redis
entrypoint: redis-server
restart: always
use hostname as localhost.
You can't use the hostname db outside the internal docker network. That would work in the applications running in the same network.
Since you exposed the db to run on port 5432, it's exposed via 0.0.0.0:5432->5432/tcp and therefore is accessible with localhost as host and port 5432