Skip to content

Commit 3866664

Browse files
Refactor database setup in Docker Compose and SQL initialization scripts
1 parent a5eb987 commit 3866664

File tree

2 files changed

+22
-34
lines changed

2 files changed

+22
-34
lines changed

prompt_cache/docker-compose.yml

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,31 @@
11
services:
22

33
db:
4-
54
image: postgres
6-
75
restart: always
8-
96
env_file:
10-
117
- .env
128

139
volumes:
14-
1510
- ./postgres-data:/var/lib/postgresql/data/
16-
1711
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
1812

1913
ports:
20-
2114
- 5432:5432
2215

2316
environment:
24-
25-
- POSTGRES_USER=${POSTGRES_USER}
26-
17+
- POSTGRES_USER=admin
2718
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
28-
29-
- POSTGRES_DB=prompt_cache_db
30-
31-
- PROMPT_CACHE_USER=prompt_cache_user
32-
33-
- PROMPT_CACHE_PASSWORD=prompt_password
19+
- PROMPT_CACHE_PASSWORD=${PROMPT_CACHE_PASSWORD}
3420

3521
networks:
36-
3722
- postgres-network
23+
command: ["postgres", "-c", "app.prompt_cache_password=${PROMPT_CACHE_PASSWORD}"]
3824

3925
volumes:
40-
4126
pgdata:
4227

4328

4429
networks:
45-
4630
postgres-network:
47-
4831
driver: bridge

prompt_cache/init.sql

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
CREATE TABLE IF NOT EXISTS prompt_cache (
2-
idx SERIAL PRIMARY KEY, -- Changed from INT to SERIAL
3-
prompt TEXT NOT NULL,
4-
response TEXT NOT NULL,
5-
llm TEXT NOT NULL,
6-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
7-
);
1+
-- Create a dedicated database for LangChain cache
2+
CREATE DATABASE langchain_cache;
83

9-
-- Create user with environment variable
10-
CREATE USER prompt_cache_user WITH PASSWORD '${PROMPT_CACHE_PASSWORD}';
4+
-- Create a role with limited privileges (default password will be overridden)
5+
DO $$
6+
BEGIN
7+
EXECUTE format('CREATE ROLE langchain_user WITH LOGIN PASSWORD %L',
8+
current_setting('app.prompt_cache_password'));
9+
EXCEPTION WHEN undefined_object THEN
10+
-- Fallback if the variable isn't set
11+
CREATE ROLE langchain_user WITH LOGIN PASSWORD 'langchain_pass';
12+
END
13+
$$;
1114

12-
-- Grant permissions
13-
GRANT CONNECT ON DATABASE prompt_cache_db TO prompt_cache_user;
14-
GRANT USAGE ON SCHEMA public TO prompt_cache_user;
15-
GRANT SELECT, INSERT ON prompt_cache TO prompt_cache_user;
16-
GRANT USAGE, SELECT ON SEQUENCE prompt_cache_idx_seq TO prompt_cache_user;
15+
-- Rest of your SQL remains the same...
16+
GRANT CONNECT ON DATABASE langchain_cache TO langchain_user;
17+
\c langchain_cache
18+
GRANT USAGE ON SCHEMA public TO langchain_user;
19+
GRANT CREATE ON SCHEMA public TO langchain_user;
20+
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO langchain_user;
21+
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO langchain_user;

0 commit comments

Comments
 (0)