Skip to content

Commit 41b3a68

Browse files
committed
created user for db
1 parent 4afc12e commit 41b3a68

File tree

6 files changed

+23
-17
lines changed

6 files changed

+23
-17
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ jobs:
4545
- name: Run script
4646
env:
4747
POSTGRES_USER: test
48-
PGPASSWORD: test
4948
DB_NAME: test_db
50-
POSTGRES_HOST: localhost
5149
working-directory: ./server/database
5250
run: chmod +x entrypoint.sh && ./entrypoint.sh
5351

compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ services:
2424
context: ./server/database/
2525
dockerfile: Dockerfile
2626
env_file:
27-
- ./postgres.env
2827
- ./db.env
28+
- ./postgres.env
2929
ports:
3030
- '5432:5432'
3131
volumes:

db.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
DB_HOST=postgresInstance
22
DB_PORT=5432
3-
DB_USERNAME=test
3+
DB_USERNAME=hello
44
DB_PASSWORD=test
55
DB_NAME=quantum

postgres.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
POSTGRES_PASSWORD=test
2-
POSTGRES_USER=test
1+
POSTGRES_USER=root
2+
POSTGRES_PASSWORD=dasjjdasjdashjdaskdhkasdhasdahsdui923d

server/database/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ RUN mkdir -p /var/log/postgres
1010
RUN chmod -R 777 /var/log/postgres && \
1111
chown postgres /var/log/postgres
1212

13+
# POSTGRES_USER must be passed as a env variable
1314
HEALTHCHECK --interval=5s --timeout=30s --start-period=5s --retries=3 \
14-
CMD pg_isready -U postgres
15+
CMD pg_isready -U $POSTGRES_USER
1516

1617
CMD ["postgres", "-c", "config_file=/docker-entrypoint-initdb.d/postgresql.conf"]

server/database/entrypoint.sh

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# POSTGRES_USER and DB_NAME must be provided as a env on docker compose
3+
# POSTGRES_USER, DB_USERNAME and DB_NAME must be provided as a env on docker compose
44

55
set -e
66

@@ -9,24 +9,31 @@ GREEN='\033[0;32m'
99
RED='\033[0;31m'
1010
ENDC='\033[0m'
1111

12+
1213
echo -e "${GREEN}Waiting for PostgreSQL to start...${ENDC}\n"
1314
until pg_isready -U $POSTGRES_USER; do
1415
echo -e "${RED}Not ready yet...${ENDC}\n"
1516
sleep 2
1617
done
1718

19+
echo -e "${GREEN}Creating new user ${DB_USERNAME}...${ENDC}\n"
20+
psql -U $POSTGRES_USER -c "CREATE DATABASE $DB_USERNAME;"
21+
psql -U $POSTGRES_USER -c "CREATE USER $DB_USERNAME WITH LOGIN PASSWORD '$DB_PASSWORD' CREATEDB;"
22+
1823
if [ ! $(psql -U $POSTGRES_USER -tc "SELECT 1 FROM pg_database WHERE datname='${DB_NAME}';") ]; then
19-
echo -e "${GREEN}Creating ${DB_NAME} database...${ENDC}"
20-
createdb -U $POSTGRES_USER $DB_NAME
24+
echo -e "${GREEN}Creating ${DB_NAME} database...${ENDC}\n"
25+
psql -U $DB_USERNAME -c "CREATE DATABASE $DB_NAME OWNER $DB_USERNAME;"
26+
psql -U $POSTGRES_USER -c "GRANT CONNECT ON DATABASE $DB_NAME TO $DB_USERNAME;"
27+
psql -U $POSTGRES_USER -c "GRANT SELECT, UPDATE, INSERT, DELETE, TRIGGER ON ALL TABLES IN SCHEMA public TO $DB_USERNAME;"
2128
fi
2229

2330
echo -e "${GREEN}Setting up tables...${ENDC}\n"
2431

25-
psql -U $POSTGRES_USER -d $DB_NAME -c "
32+
psql -U $DB_USERNAME -d $DB_NAME -c "
2633
CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";
2734
"
2835

29-
psql -U $POSTGRES_USER -d $DB_NAME -c "
36+
psql -U $DB_USERNAME -d $DB_NAME -c "
3037
CREATE TABLE IF NOT EXISTS backends (
3138
backend_name VARCHAR(30) NOT NULL PRIMARY KEY,
3239
id uuid NOT NULL DEFAULT gen_random_uuid(),
@@ -35,7 +42,7 @@ CREATE TABLE IF NOT EXISTS backends (
3542
);
3643
"
3744

38-
psql -U $POSTGRES_USER -d $DB_NAME -c "
45+
psql -U $DB_USERNAME -d $DB_NAME -c "
3946
CREATE TABLE IF NOT EXISTS jobs (
4047
id uuid NOT NULL PRIMARY KEY,
4148
pointer serial NOT NULL,
@@ -49,7 +56,7 @@ CREATE TABLE IF NOT EXISTS jobs (
4956
);
5057
"
5158

52-
psql -U $POSTGRES_USER -d $DB_NAME -c "
59+
psql -U $DB_USERNAME -d $DB_NAME -c "
5360
CREATE TABLE IF NOT EXISTS result_types (
5461
id uuid NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
5562
job_id uuid NOT NULL REFERENCES jobs(id) ON DELETE CASCADE,
@@ -59,7 +66,7 @@ CREATE TABLE IF NOT EXISTS result_types (
5966
);
6067
"
6168

62-
psql -U $POSTGRES_USER -d $DB_NAME -c "
69+
psql -U $DB_USERNAME -d $DB_NAME -c "
6370
CREATE TABLE IF NOT EXISTS results (
6471
id uuid NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
6572
job_id uuid NOT NULL REFERENCES jobs(id) ON DELETE CASCADE,
@@ -69,7 +76,7 @@ CREATE TABLE IF NOT EXISTS results (
6976
);
7077
"
7178

72-
psql -U $POSTGRES_USER -d $DB_NAME -c "
79+
psql -U $DB_USERNAME -d $DB_NAME -c "
7380
COMMENT ON COLUMN backends.plugin is 'The name of the python plugin used for this specific backend';
7481
COMMENT ON COLUMN backends.pointer is 'The pointer holds the order a value was inserted. This is useful for getting data using cursors.';
7582
COMMENT ON COLUMN jobs.qasm is 'The path of a .qasm file';
@@ -84,4 +91,4 @@ COMMENT ON COLUMN results.expval is 'When results_types.expval is TRUE, the resu
8491
"
8592

8693

87-
echo -e "${GREEN}Finished SETUP${ENDC}"
94+
echo -e "${GREEN}Finished SETUP${ENDC}\n"

0 commit comments

Comments
 (0)