Docker file postgres initial schemas not executed - postgresql

I updated my docker file to version 3 but now the inicial schemas are not being created. i already tried with a different volume
I run it with : docker-compose -f docker-compose.yml up
version: '3'
services:
db-service:
image: postgres:11.2
volumes:
- ./dbscripts/:/docker-entrypoint-initdb.d/
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=sample
ports:
- 5432:5432

Check these 2 things:
The init scripts are triggered only on the first deploy. On the subsequent docker-compose ups you probably see as well on the logs the following message:
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
To force the re-initialization, you can wipe the data volume(This will delete the entries, not only the schema).
To find the path, simply docker inspect <postgres-container-id> and look for the HostConfig.Binds
The script executes till the first type "longvarbinary" does not exist error. You will need to fix this.
db-service_1 | CREATE TABLE
db-service_1 | 2020-07-26 19:29:34.121 UTC [68] ERROR: type "longvarbinary" does not exist at character 68
I opened a console in the postgres container and your sample database is there, as well as a table that is created before the .sql error occurs in the script:
docker exec -ti 22b0bb87561f sh
# psql -U postgres
psql (11.2 (Debian 11.2-1.pgdg90+1))
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
sample | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# \c sample
You are now connected to database "sample" as user "postgres".
sample=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------------------+-------+----------
public | oauth_client_details | table | postgres
(1 row)

I ran into the same issue, and I figured it was because I forgot to clear the volume when I take down docker compose with command docker-compose down.
The solution that helps is when you take down docker compose, you specify that all volumes must be removed docker-compose down --volumes. Also, make sure to check and delete any related volumes using docker volume ls.
WARNING: Any subsequent command docker-compose down should not have the flag --volume or else your persistent data will be wiped out. This may or may not be desirable depending on your use case. The idea is to ONLY run the init script on the very first time you run docker-compose up. Any subsequent command up after that will skip initialization because data is already populated, and you don't want to override it.
Hope it helps!

Related

Can not connect to a postgres database in a docker container from my local machine

I have a strange problem with docker and postgres. I can not connect from my local machine to psql inside the docker container.
First of all I created my container with this command:
docker run -d -p 5432:5432 --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres
After that I executed the container with following command:
docker exec -it my-postgres bash
Inside the container I started psql with the postgres user:
psql -U postgres
And I created a new database:
CREATE DATABASE mytestdb;
Everything works inside the container, but if I want to connect to it from my machine it does only show the database from my machine. I tried this to connect to postgres:
psql -h localhost -p 5432 -U postgres
Which should me show something like this:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
mytestdb | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
But instead I get this:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------------+-----------------+----------+---------+---------+----------------------------------
user | user | UTF8 | C | C |
postgres | user | UTF8 | C | C |
template0 | user | UTF8 | C | C | =c/user +
| | | | | user =CTc/user
template1 | user | UTF8 | C | C | =c/user +
| | | | | user =CTc/user
(4 rows)
(END)
In every tutorial, I saw they did it, but for me, it doesn't work. So please help me.
The problem is when you creating the db.
CREATE DATABASE mytestdb you forgot to add ; the command should be like CREATE DATABASE mytestdb;
And then you have your db and you can access it from inside the container or your host with these commands

relation *db_name* does not exist postgres

I am trying to set up an ubuntu server with postgres(following digitalocean tuorial).
I ssh'd to the server as root user, from there created a user named 'justin', gave that user admin privileges as instructed. Switched to 'justin', installed python, django, pip, etc and postgres. From here the tutorial said type sudo -u postgres psql
, I did this. From here I created a db 'jobzumodb' and user 'jobzumojustin'. I am now trying to grant privileges of that db to this new user: GRANT ALL PRIVILEGES ON jobzumodb TO jobzumojustin; and it returns ERROR: relation "jobzumodb" does not exist. However if I postgres=# \l I see:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+---------+-----------------------
jobzumodb | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | admin=CTc/postgres
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
Anyone know what is causing this?
Note: when I created the db I created it with: CREATE DATABASE jobzumoDB;. And was trying to access it for a long time as 'jobzumoDB' and not 'jobzumodb', before learning the \l command and that it saved as 'jobzumodb' because I did not pass it in quotes. Anyway, not sure if this could be having an effect.
Thanks for any help.
Another edit: upon rereading this I realized I am using the 'postgres' user (I see postgres=#), should I be using the 'jobzumojustin' user and jobzumojustin=# ? Also, I checked to see if 'jobzumojustin' exists, it does, but does not have anything listed under 'list of roles and attributes'.
I think you forgot the database keyword. It should be:
GRANT ALL PRIVILEGES ON DATABASE jobzumodb TO jobzumojustin;

psql saying database does not exist but it does exist in pgadmin

I have just installed Postgres v10.4 on Windows and created, using pgadmin, a new database called analysis. It is there, I can see it in pgadmin, and it has one table in it. However, I cannot connect to this database using psql.
C:\WINDOWS\system32>psql -d postgres -U postgres
psql (10.4)
WARNING: Console code page (850) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=# l
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-----------------------------+-----------------------------+-----------------------
postgres | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 |
template0 | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres-# \q
C:\WINDOWS\system32>psql -d analysis -U postgres
psql: FATAL: database "analysis" does not exist
C:\WINDOWS\system32>
I don't know what I'm doing with psql, but all I've found is that I can connect to db postgres as user postgres. There's a \l command and it shows 3 databases (2 of which, template0 and template1) I can't even see in pgadmin.
How can I connect to the database (analysis) that I've just created in pgadmin? Can anybody explain what user I'm logged in under in pgadmin, if it's different to 'postgres' and if it isn't, how is it that I can't see in psql what I can see in pgadmin?
template0 and template1 are know as skeleton databases. When you use CREATE DATABASE command postgres copy the existing databae.
By default template1 database is used to create new database.
I think your pgadmin and psql are connected to different cluster. A cluster in postgresql is collection of one or more databases in a single instance of server
The image you posted does not show the complete view of pgadmin browser. But
if there are 2 cluster then you can see in pgadmin like "Servers(2)". To find running port of each cluster right click on corresponding cluster and select properties then click connection tab. Here port number can be seen.
Then connect to cluster in psql using that Port.

GitLab setup using official PostgreSQL and GitLab docker container not working

I am trying to setup GitLab using separate docker containers for both GitLab and PostgreSQL. I am using RancherOS v1.0.3 with Kubernetes enabled. For now, all i want is to have a single node having both the containers. Later, i will look into configuring them on separate nodes.
[rancher#rancher-agent-2 ~]$ cat postgresql.sh
docker run --name=postgresql -d \
--env 'DB_NAME=gitlabhq_production' \
--env 'DB_USER=gitlab' --env 'DB_PASS=password' \
--env 'DB_EXTENSION=pg_trgm' \
--volume /opt/postgresql:/var/lib/postgresql \
postgres:9.6.3
[rancher#rancher-agent-2 ~]$ cat gitlab.sh
docker run --name=gitlab -d --link postgresql:postgresql \
-v /opt/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:9.3.9-ce.0
Now when i run the PostgreSQL container and try logging in, i get errors.
[rancher#rancher-agent-2 ~]$ docker exec -it postgresql bash
root#a6cef780c594:/# psql -d gitlabhq_production -U gitlab
psql: FATAL: role "gitlab" does not exist
Seems the db (gitlabhq_production) has not been created.
root#a6cef780c594:/# psql -U postgres
psql (9.6.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
Why is it not creating the db despite passing the parameters?
UPDATE:
PostgreSQL container log:
[rancher#rancher-agent-2 ~]$ docker logs postgresql
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
...
...
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
done
server started
ALTER ROLE
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
...
...
You are using the wrong environment vars names:
DB_NAME should be POSTGRES_DB
DB_USER should be POSTGRES_USER
DB_PASS should be POSTGRES_PASSWORD
Check https://hub.docker.com/_/postgres/

PostgreSQL: Drop Database but DB is still there [duplicate]

This question already has answers here:
In psql, why do some commands have no effect?
(2 answers)
Closed 2 years ago.
I am new to PostgreSQL and I try to get my head around it. I am familiar to db's and MySQL.
I am trying to delete database, which I created since psql seems to ignore the changes I try to push through Django.
When I execute \l I get the following response:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------------+--------+----------+-------------+-------------+-------------------
postgres | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
test_db | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
template0 | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 | =c/neurix +
| | | | | neurix=CTc/neurix
template1 | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 | =c/neurix +
| | | | | neurix=CTc/neurix
template_postgis | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
(5 rows)
Now I wan to drop the database "test_db" with
DROP DATABASE test_db
but when I execute \l afterwards, the table is still there and the overview looks like about.
Did you type a ; after the DROP DATABASE test_db? Did PostgreSQL print a response to your command?
I had a similar issue when working on a Rails 6 application in Ubuntu 20.04 with PostgreSQL as my database.
When I run the command:
DROP DATABASE my-db;
The database is dropped successfully, however, the schema for the database is still left.
So when I run the command:
CREATE DATABASE my-db;
And I check the tables in the newly created database, I realized they still contained the same tables as the previously deleted database, even though I have not run any migration.
Here's how I fixed it:
Instead of running the command:
DROP DATABASE my-db;
run the command:
DROP DATABASE IF EXISTS my-db;
This deletes the database and it's corresponding schema.
That's all.
I hope this helps