Docker and Postgis - How do I access shp2pgsql inside my docker container? - postgresql

I am running postgres in a docker container using docker-compose and it spins up with no issue and I am able to connect to the database. But now I want to go into the container and execute the postgis shp2pgsql to load a shape file but the command seems to be nonexistent. Below is my code:
docker-compose.yaml
version: '3'
services:
db:
container_name: pg_container
image: postgis/postgis
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: test_db
volumes:
- ./data:/var/lib/postgresql/
- ./postgres_init:/postgres_init
ports:
- 5433:5433
networks:
- ch_ntw
networks:
ch_ntw:
driver: bridge
ipam:
config:
- subnet: 10.222.1.0/24
Getting into the container:
docker exec -it pg_container bash
Connecting to the db without issue using psql:
psql --host=pg_container --dbname=test_db --username=root
But then if I try to invoke shp2pgsql from bash I get the following:
shp2pgsql -s 2263:4326 postgres_init/nyct2010_15b/nyct2010.shp | psql -d test_db
bash: shp2pgsql: command not found
I would think since this is a postgis container that the function should be accessible no?

shp2pgsql is a client package. The postgis/postgis image is the PostGIS server components only. If you want to use shp2pgsql or other client tools, install them locally on your host, or in another container.

Related

Error: timeout expired on trying to connect in Docker Postgres using pgAdmin

I've been created a docker container of postgres service, but on start it and try to connect in database I get erros like I didn't defined a user and database to Postgres instance, I already tried to change the docker-compose and find the poblem but I didn't find.
Follow the attachments:
Dockerfile:
FROM wyveo/nginx-php-fpm:latest
RUN chmod -R 775 /usr/share/nginx/
RUN export pwd=pwd
docker-compose.yml:
version: '3'
services:
laravel-app_prm:
build: .
ports:
- "8099:80"
volumes:
- ${pwd}/.docker/nginx/:/usr/share/nginx
postgres_prm:
image: postgres
restart: always
environment:
- POSTGRES_USER=db_usr
- POSTGRES_PASSWORD=postgres_password
- POSTGRES_DB=db_prm
ports:
- "5432:5440"
volumes:
- ${pwd}/.docker/dbdata:/var/lib/postgresql/data/
**when I try to connect to the database directly through the container's bash, I get an error that user and database, both being inserted in the same way as defined in docker-compose.yml, do not exist.
sudo docker container <postgres_container_id> bash
psql -h localhost -U db_usr
... and so on...
And to set up connection in pgAdmin I got the container IP using:
sudo docker container inspect <postgres_container_id>
and getting the value from IPAddress atribute.

Docker Postgres and Adminer accept connection but doesn't work

On my rasperry pi 4 I've installed docker and docker-compose and now I'm tring to install and use Postgres and Adminer
following that https://hub.docker.com/_/postgres I've created docker-compose.yaml file as follow:
# Use postgres/example user/password credentials
version: '3.1'
services:
db:
image: postgres
restart: unless-stopped
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
adminer:
image: adminer
restart: unless-stopped
ports:
- 8080:8080
and i run it with
docker-compose -f docker-compose.yaml up -d
after that DB_1 starts and adminer too
but when i try connect to http://192.168.1.38:8080/ i can't reach it
even if i try connect to postgres through pgAdmin it's says
could not connect to server: Connection refused (0x0000274D/10061) Is
the server running on host "192.168.1.38" and accepting TCP/IP
connections on port 5432?
however if i don't use docker-compose but just
docker run --name postgres -d --restart unless-stopped -p 5432:5432 -e POSTGRES_PASSWORD=123456 -v ${PWD}/data:/var/lib/postgresql/data postgres
it's work through pgAdmin
do you know what i'm doing wrong?
UPDATE: seems the problem is with docker-compose because any kind of docker-compose.yml file block connection to it...
with a container with djgango i tried to start server and it's works but when i try reach page it seem bloccked too
when i run docker-compose.yaml file docker-compose ps output is:
sudo netstat -tulpn screenshot
PgAdmin can't reach 5432 ports because you don't expose it.
Like for Adminer you need to expose the Postgres port 5432 on your machine in your compose file.
version: '3.1'
services:
db:
image: postgres
restart: unless-stopped
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
adminer:
image: adminer
restart: unless-stopped
ports:
- 8080:8080
a little late to the party but what you need to do is figure out the IP address of the postgres container, and use that as your host.

Docker compose cannot run command service not running

I created docker-compose.yml which content you can find below. I navigate to the folder where file resist and run command:
docker-compose up -d
This was shown:
Starting postgres ... done
then i run that query:
docker-compose ps
Result:
Name Command State Ports
---------------------------------------------------------
postgres docker-entrypoint.sh postgres Exit 1
Now i wanted to run some command:
docker exec -it postgres psql -h localhost -p 54320 -U robert
This is what i get:
Error response from daemon: Container ae1565a84bcf0b3662b47d4f277efd2830273554b6bcf4437129e33b31c88b35 is not running
Is my container not running or? please of support.
docker-compose.yml:
version: "3"
services:
# Create a service named db.
db:
# Use the Docker Image postgres. This will pull the newest release.
image: "postgres"
# Give the container the name my_postgres. You can changes to something else.
container_name: "postgres"
# Setup the username, password, and database name. You can changes these values.
environment:
- POSTGRES_USER=robert
- POSTGRES_PASSWORD=robert
- POSTGRES_DB=mydb
# Maps port 54320 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
ports:
- "54320:5432"
# Set a volume some that database is not lost after shutting down the container.
# I used the name postgres-data but you can changed it to something else.
volumes:
- ./volumes/postgres:/var/lib/postgresql/data
Can you attempt exec
docker run -it postgres psql -h localhost -p 54320 -U robert
?
$ docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Since your container has the status exit, you can't use docker exec
Can you use this docker-compose file?
version: "3"
volumes:
postgres_app: ~
services:
# Create a service named db.
postgres:
image: "postgres"
environment:
POSTGRES_USER: robert
POSTGRES_PASSWORD: robert
POSTGRES_DB: "mydb"
volumes:
- "postgres_app:/var/lib/postgresql/data"
ports:
- "54320:5432"
restart: always
And this command docker-compose exec postgres psql -U robert -d mydb
I hope this will help!
On my computer i executed this file

PostgreSQL Container in Docker Not Authorizing a Correct Password

I have arranged a node.js back end to connect to a redis cache and psql database.
The app I have created is running but I would like to do some database admin and have attempted to log in using pgAdmin - however, my details were rejected.
I thought it might be a pgAdmin thing so I attempted to use the login URI in powershell but again it was rejected.
I checked that the psql service is running on the exposed port (in case I messed up the docker-compose config) and it is...not sure where to go from here.
My docker-compose config for the database is:
# PostgreSQL
postgres:
container_name: postgres
build: ./postgres
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
POSTGRES_URL: postgres://admin:password#localhost:5432/myapp
POSTGRES_DB: myapp
POSTGRES_HOST: postgres
ports:
- "5432:5432"
I should note that the database is running - I can log in to my front end and access data, etc...
My login attempt:
psql postgres://admin:password#localhost:5432/myapp
And the response:
psql: FATAL: password authentication failed for user "admin"
I think you docker-compose not formatted well if it's not copy-paste issue as the environment variable, not place properly.
# PostgreSQL
postgres:
image: postgres
container_name: postgres
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
POSTGRES_URL: postgres://admin:password#localhost:5432/myapp
POSTGRES_DB: myapp
POSTGRES_HOST: postgres
ports:
- "5432:5432"
Or you can try
version: '3.7'
services:
postgresdb:
container_name: postgres
environment:
POSTGRES_DB: appdb
POSTGRES_USER: appdb
POSTGRES_PASSWORD: 123123
image: bitnami/postgresql:latest
ports:
- "5432:5432"
Or better to post you Dockerfile, as I see your building your own Docker image, but better to use the offical image of Postgres like the one I posted above.
Also will suggest debugging on container DB first and verify connectivity on the container localhost, debugging and testing with depended containers like connecting from nodejs first here one lost in the actual problem.
Check if your ENV set properly.
docker exec postgres bash -c "printenv "
or
docker exec postgres bash -c "printenv | grep POSTGRES_"
or
docker exec -it postgres bash -c "psql -U admin myapp"

Docker volume doesn't keep data after turned docker-compose down

I am using docker compose to combine 2 images (tomcat with my app and database - postgres).
My compose file looks like this :
version: '3'
services:
tomcat:
build: ./tomcat-img
ports:
- "8080:8080"
depends_on:
- "db"
db:
build: ./db-img
volumes:
- db-data:/var/lib/postgres/data
ports:
- "5433:5432"
volumes:
db-data:
and here is dockerfile for database image:
FROM postgres:9.5-alpine
ENV POSTGRES_DB mydb
ENV POSTGRES_USER xxxx
ENV POSTGRES_PASSWORD xxxx
COPY init-db.sql /docker-entrypoint-initdb.d/
EXPOSE 5432
CMD ["postgres"]
Next I started my containers with docker-compose cli docker-compose -f docker-compose.yml up
and run psql tool with:
docker exec -it container_id psql -d xxxx -U xxxx
and insert new record. After that I check if there really is:
select * from my_table;
After that I tried stopped docker compose and remove containers with:
docker-compose -f docker-compose.yml down
and start it again
docker-compose -f docker-compose.yml up
when I run again psql tool of db container and select data in my_table, there is no previous inserted record ... Can you help me to fix it please? I need init my db with init-db.sql just once and next using that persist storage. Thanks for answers.
In my dockerized Postgresql with a data volume I am binding to /var/lib/postgresql and not to /var/lib/postgres/data. Try changing your compose file to
volumes:
- db-data:/var/lib/postgresql