-
Notifications
You must be signed in to change notification settings - Fork 0
Dev Environment ‐ Setup Tools
To test docker instance is running successfully.
docker run hello-world
docker pull postgres
docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
read
- Data Source: Bank Customer Churn - https://www.kaggle.com/datasets/radheshyamkollipara/bank-customer-churn
Steps to import
To import CSV data into a PostgreSQL database running in a Docker container, follow these steps:
If you don't already have a PostgreSQL container running, start one:
docker run --name my-postgres -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mysecretpassword -e POSTGRES_DB=bankmaster -p 5432:5432 -d postgres
This starts a PostgreSQL container with:
-
User:
myuser
-
Password:
mypassword
-
Database:
mydatabase
-
Port:
5432
If your CSV file is on your local machine, copy it into the container:
docker cp mydata.csv my-postgres:/mydata.csv
Alternatively, mount a local directory when running the container:
docker run --name my-postgres -v $(pwd)/data:/data -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mysecretpassword -e POSTGRES_DB=bankmaster -p 5432:5432 -d postgres
Then place the CSV file inside the data/
folder.
Access the running PostgreSQL container:
docker exec -it my-postgres psql -U myuser -d mydatabase
Ensure the table structure matches your CSV data:
CREATE TABLE mytable (
id SERIAL PRIMARY KEY,
name TEXT,
age INT,
city TEXT
);
Run the following command inside PostgreSQL:
COPY mytable(name, age, city)
FROM '/mydata.csv'
DELIMITER ','
CSV HEADER;
-
DELIMITER ','
→ Specifies that values are comma-separated. -
CSV HEADER
→ Skips the first row if it contains column headers.
Check if the data was imported successfully:
SELECT * FROM mytable;
If the CSV is on your local machine and PostgreSQL is accessible outside Docker:
cat mydata.csv | docker exec -i postgres-container psql -U myuser -d mydatabase -c "COPY mytable FROM STDIN WITH CSV HEADER DELIMITER ',';"
-
Permission Denied? Run:
ALTER TABLE mytable OWNER TO myuser;
- Wrong File Path? Ensure the file is inside the container.
-
Encoding Issues? Convert the file to UTF-8 using:
iconv -f ISO-8859-1 -t UTF-8 mydata.csv -o mydata_utf8.csv
If your interactive PostgreSQL session inside Docker is not working, here are possible reasons and solutions:
First, check if your PostgreSQL container is running:
docker ps
If you don’t see your container, start it:
docker start postgres-container
Or, if you haven't created one yet:
docker run --name postgres-container -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mypassword -e POSTGRES_DB=mydatabase -p 5432:5432 -d postgres
Try accessing PostgreSQL using:
docker exec -it postgres-container psql -U myuser -d mydatabase
If this doesn’t work, check logs for errors:
docker logs postgres-container
If docker exec
isn’t working, enter the container using bash:
docker exec -it postgres-container bash
Then manually run:
psql -U myuser -d mydatabase
If psql
is not found, PostgreSQL might not be running inside the container. Check with:
pg_isready
If it’s not ready, restart the service inside the container:
service postgresql restart
or
pg_ctl -D /var/lib/postgresql/data start
If the service is failing, inspect logs inside the container:
docker logs postgres-container | tail -n 20
If you're connecting from outside the container, check if PostgreSQL is listening on port 5432:
docker inspect postgres-container | grep 5432
Or from inside the container:
netstat -tulnp | grep 5432
If it’s not listening, try:
docker restart postgres-container
If you see authentication errors, edit pg_hba.conf
inside the container:
docker exec -it postgres-container bash
nano /var/lib/postgresql/data/pg_hba.conf
Ensure it contains:
host all all 0.0.0.0/0 md5
Then restart PostgreSQL:
service postgresql restart
If docker exec
isn’t working, try connecting from your host:
psql -h localhost -U myuser -d mydatabase -p 5432
If that fails, ensure PostgreSQL is accepting external connections by editing postgresql.conf
:
docker exec -it postgres-container bash
nano /var/lib/postgresql/data/postgresql.conf
Change:
listen_addresses = '*'
Then restart PostgreSQL:
service postgresql restart
-
Check container status:
docker ps -a
-
Restart PostgreSQL inside container:
service postgresql restart
-
Run
psql
manually inside container:docker exec -it postgres-container psql -U myuser -d mydatabase
-
Inspect logs for errors:
docker logs postgres-container | tail -n 20
Let me know what error you’re facing, and I can help troubleshoot further! 🚀