Making sure retail_db is either mounted or copied on to the Docker Container.
docker run --name retail_pg -e POSTGRES_PASSWORD=mypassword -v /Users/fallout/PycharmProjects/Internal/data_copy/retail_pg:/var/lib/postgresql/data -v /Users/fallout/research/data/retail_db_json:/data -p 5432:5432 postgres
CREATE DATABASE retail_db;
CREATE USER retail_user WITH ENCRYPTED PASSWORD 'something';
GRANT ALL PRIVILEGES ON DATABASE retail_db TO testing;
After which I run:
docker exec -it retail_pg psql -U testing -d retail_db -f /data/create_db_tables_pg.sql
Results in:
psql:/data/create_db_tables_pg.sql:12: ERROR: permission denied for schema public
LINE 1: CREATE TABLE departments (
Any ideas how to fix this ?
GRANT ALL PRIVILEGES ON SCHEMA PUBLIC TO user_name;
Above is the solution to fix the issue. #Turing85 answered but there is an error in the syntax.
Related
I used to create postgresql database for my old project with command:
docker run --name oldpostgresqldb -e POSTGRES_USER=oldadmin -e POSTGRES_PASSWORD=secret -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres
Then create database with command:
docker exec -i oldpostgresqldb psql -U oldadmin -c "CREATE DATABASE oldDB WITH ENCODING='UTF8' OWNER=oldadmin;"
When I started my new project stoped and removed all containers, images, volumes etc. and even used docker system prune.
Now I am trying to create new container and db with commands:
docker run --name newpostgresqldb -e POSTGRES_USER=newadmin -e POSTGRES_PASSWORD=secret -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres
and
docker exec -i newpostgresqldb psql -U newadmin -c "CREATE DATABASE newDB WITH ENCODING='UTF8' OWNER=newadmin;"
But I receive psql: error: FATAL: role "newadmin" does not exist.
Furthermore, I still can manage oldDB with oldadmin user in newpostgresqldb container, because user oldadmin is still exists.
How can I delete old data and create new user and database using docker?
Thanks to #Ay0 I've found a solution. I just needed to clear content of /data folder in the host directory manualy.
I'm new with docker and i have to learn at my new job, so i should restore a sql file into a postgres container, when i type the command:
docker exec -i postgres-container pg_restore -U postgres -d postgres /var/lib/postgresql/data/_postgres_2020-11-09T02_00_06Z.sql
i get the following message:
pg_restore: error: input file appears to be a text format dump. Please use psql.
After that i tried to convert that file with psql inside the container but i get this message:
psql: error: FATAL: role "root" does not exist.
How can i get this? can someone help me ?
I got it! activereality's answer solve my problem in this post Backup/Restore a dockerized PostgreSQL database
cat your_dump.sql | docker exec -i your-db-container psql -U postgres -d dbname
I have a dump file generated by using pg_dump from a remote database. I am using the file to restore and populate a database using psql.
I did not have the user and database name. I peeked into the sql file and figured out the user and database.
Here are my steps:
1. psql -h <> -u <> -d ( I used the superuser here).
2. created the user as the application needs to access using this user only.
3. changed the owner of the database to the user created in step 2 using Pgadmin.
I am unable to access any tables and getting the error -- ERROR: permission denied for table
Please Help !
Thanks
It is better if you use dump and restore instead of psql for restoring the database.
e.g
e.g suppose you have database dbname_source in remoteDB and dbname_target in localDB then you can use the below commands for dump and restore
./pg_dump -U postgres -p 5432 -h <remote_IP> -Ft dbname_source > dump_of_source.sql.tar
./pg_restore -U postgres -h localhost -p 5432 -d dbname_target dump_of_source.sql.tar
I have installed postgresql on ubuntu using:
$ sudo apt install postgresql
Now, I have a series of sql queries I would like to fire to create schemas and users and tables etc. I have put those queries in a .sql file as below:
$ sudo nano postgressetup.sql
CREATE SCHEMA schma;
CREATE USER a2i WITH PASSWORD 'password';
GRANT CONNECT ON DATABASE postgres TO schma;
This file has all the queries. I tried something like:
$ psql -U postgres -d postgres -a -f postgressetup.sql
and received error:
psql: FATAL: Peer authentication failed for user "postgres"
I want to know the way I can execute this .sql file.
Note: I've just installed postgres and no further operation is done on it. Any help is appreciated.
You can use the following command explicitly providing db context user
sudo -u postgres psql -U postgres -d postgres -a -f postgressetup.sql
I usually attach to postgres by typing in the terminal:
psql -U user db
I want to move postgres into docker container:
docker run -v /var/lib/postgresql:/var/lib/postgresql -p 5432:5432 postgres
Then I run command and have an error:
psql -h localhost -p 5432 -U user
psql: FATAL: role "user" does not exist
I expected that -v /var/lib/postgresql:/var/lib/postgresql will make possible to reach db by user. But it doesn't happen. How to make it correctly?