How to restart a stopped docker container? - postgresql

I have pulled a postgres image and created a docker container called pgdb , which has exited. Here is what the terminal returns after typing docker ps -all:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e62fdb45c727 postgres "docker-entrypoint.s…" 19 hours ago Exited (1) 14 minutes ago pgdb
now I am trying to restart my container by typing docker exec -it pgdb bash, however I am getting the following error message: Error response from daemon: Container e62fdb45c727baf9ca9d7b55401f870b35959a10f356a401f058f2e693adc2fd is not running
I tried to attach the container like so:
random#random-142:~$ sudo docker start pgdb
pgdb
random#random-142:~$ sudo docker attach pgdb
You cannot attach to a stopped container, start it first
but it also didnt work. Does anyone know how I could go about solving this? I am really desperate.
EDIT
container logs
random#random-142:~$ sudo docker logs pgdb
Error: Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
connections without a password. This is *not* recommended.
See PostgreSQL documentation about "trust":
https://www.postgresql.org/docs/current/auth-trust.html
Error: Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
connections without a password. This is *not* recommended.
See PostgreSQL documentation about "trust":
https://www.postgresql.org/docs/current/auth-trust.html

The docker container logs itself are self-explanatory. You need to specify the database and superuser password.
Add the following arguments while running your Postgres docker container:
-e "POSTGRES_DB=YOUR_DB_NAME" -e "POSTGRES_PASSWORD=YOUR_PASSWORD"
Example:
docker run -itd -p 5432:5432 -e "POSTGRES_DB=testdb" -e "POSTGRES_PASSWORD=password" --name pgdb postgres
If you are not willing to add any password, then use "POSTGRES_HOST_AUTH_METHOD=trust". Example:
docker run -itd -p 5432:5432 -e "POSTGRES_DB=testdb" -e "POSTGRES_HOST_AUTH_METHOD=trust" --name pgdb postgres

To restart the container you can use this command
docker start -ai "container_name"

Related

Problem with mounting docker and postgres on golang

I am trying to mount a container in docker with postgres sql, I tried to remove the password to see if it could start the container but it still keeps throwing me the following error:
Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
connections without a password. This is *not* recommended.
See PostgreSQL documentation about "trust":
https://www.postgresql.org/docs/current/auth-trust.html
My docker file is structured like this
FROM postgres:15.1
COPY up.sql /docker-entrypoint-initdb.d/1.sql
CMD ["postgres"]
I have tried launching the command docker run -p 54321:5432 db POSTGRES_HOST_AUTH_METHOD=trust in the console.

cannot connect to local postgres container

I am new to docker and postgres and this is really puzzling to me. After pulling the postgres 14.1 alpine image, I created a container from the image called postgres14.1 setting the user and password:
docker run --name postgres14.1 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:14.1-alpine
Then I created a database called 'simple_bank' on the container:
docker exec -it postgres14.1 createdb --username=root --owner=root simple_bank
Before connecting to the database in TablePlus or VSCode with postgres extension, I tried to access in the terminal:
docker exec -it postgres14.1 psql simple_bank
And it works fine. In the terminal , \du+ confirms root is indeed a superuser in the simple_bank database:
Role name | Attributes | Member of | Description
-----------+------------------------------------------------------------+-----------+-------------
root | Superuser, Create role, Create DB, Replication, Bypass RLS | {} |
Then I tried to connect via TablePlus with the following config:
Host/Socket: 127.0.0.1
Port:5432
User:root
Password:secret
Database:root
This can't be connected saying role root doesn't exit. If leave the user blank and set database to postgres then it connects but it can't find the simple_bank database created from the command line.
In the end, the problem is because I already have a local instance of postgres installed and it by default also connects to port 5432 on my local machine. Hence in creating the postgres container, I need to choose a different port on local host.
So changing the below docker command and then connect locally using port 5433 fixes the problem.
docker run --name postgres14.1 -p 5433:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:14.1-alpine

Access Postgres database container but get error

I use docker to run a Postgres database container. My command to start it is:
docker run --name postgres-me -e POSTGRES_PASSWORD=password -d -p 5432:5432 postgres:alpine
Then it is up and running:
~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b662971d9b54 postgres:alpine "docker-entrypoint.s…" 6 seconds ago Up 3 seconds 0.0.0.0:5432->5432/tcp postgres-me
Then I try to access the container by:
~ $ docker exec -it b662971d9b54 bin/bash
bash-5.0# psql
psql: error: could not connect to server: FATAL: role "root" does not exist
As you can see above, after I go into the container, then I tried command psql, but I got error could not connect to server: FATAL: role "root" does not exist
How to get rid of this error?
You need to provide username as well.
Try this :
psql -U <username>

how to change Postgresql docker image password

I have created a Postgresql docker image using the following command on Windows:
docker run --name airdb-postgres -e POSTGRES_PASSWORD=post1234 -d -p 5432:5432 postgres:alpine
is it possible to change the password I assigned to it, or I should create a new one by disposing this image?
You should be able to do that by logging into the container
docker exec -it <container_id> bash
then use psql cli to change the password.
See How to change PostgreSQL user password? for the latter part.

How to properly set VOLUME and CMD instructions in Postgres Dockerfile?

I have a working Postgres Dockerfile that I modify and unfortunately after applying modifications Postgres container stops working as expected. I'd like to ask your for explanation of what I'm doing wrong.
Working example
Here's the Postgres Dockerfile that works and which I modify:
# Use ubuntu image
FROM ubuntu
# Install database
RUN apt-get update && apt-get install -y postgresql-9.3
# Switch to postgres user.
USER postgres
# Create databse and user with all privileges to the database.
RUN /etc/init.d/postgresql start && \
psql --command "CREATE DATABASE docker;" && \
psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
psql --command "GRANT ALL PRIVILEGES ON DATABASE docker TO docker;"
# Allow remote connections to the database.
RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
# Add VOLUMEs to allow backup of config, logs and databases
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
# Set the default command to run when starting the container
CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]
I build it like that:
docker build --tag postgres-image .
Then I create a container:
docker run -d -it -p 32768:5432 --name=postgres postgres-image
And I connect with database:
psql -h localhost -p 32768 -d docker -U docker --password
First modification
I don't need to have any volumes because I'm going to use data-only container that will store all Postgres data. When I remove the line:
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
and do all steps like in working example I get the following error after passing password in the last step:
psql: FATAL: the database system is starting up
FATAL: the database system is starting up
So the question is: Why do I need VOLUME instruction in the Dockerfile?
Second modification
This modification doesn't include the first one. Both modification are independent.
The parameters used in CMD instraction points to default Postgres data directory and configuration file so I wanted to simplify it by setting CMD to the command I always use to start Posgres:
service postgres start
After setting CMD to:
CMD ["service", "postgres", "start]
and doing all steps like in working example I get the following error after passing password in the last step:
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 32768?
The question is: Why the command that works on my host system doesn't work in Docker container?
I'm not sure about the first problem. It may be that Postgres doesn't like running on top of the UFS.
The second problem is just that a container will exit when its main process ends. So the command "service postgres start" runs, starts Postgres in the background then immediately exits and the container halts. The first version works because Postgres stays running in the foreground.
But why are you doing this? Why not just use the official Postgres image?