TIL: PostgreSQL Insert ID Only Works With id as Primary Key
I’ve created a database table for my PostgreSQL database with Nim:
import db_postgres, os ## some code proc setup*(database: Database) = database.db.exec(sql""" CREATE TABLE IF NOT EXISTS Url( shortcode SERIAL PRIMARY KEY, orig_url VARCHAR(255) NOT NULL ); """) ## more code My primary key is shortcode as a SERIAL data type. That means the shortcode column automatically increments.
I want to insert data into the database and return the generated ID for the row.
Setup a Postgres Database With Docker Compose
Adding a Postgres database to an existing service is fairly straightforward with docker-compose.
Create a Dockerfile for your main service. For example, here’s a simple Docker container with Node:
FROMnode:12.14.0-alpine3.9ENV NPM_CONFIG_PREFIX=/home/node/.npm-globalRUN npm install npm@latest -g –silentWORKDIR/opt/appCOPY package*.json /opt/app/RUN npm install –no-optional && npm cache clean –forceENV PATH /opt/app/node_modules/.bin:$PATHENV PATH=$PATH:/home/node/.npm-global/binARG NODE_ENV=production ENV NODE_ENV $NODE_ENVARG PORT=3000 ENV PORT $PORTEXPOSE$PORT 8229 8230COPY . .CMD ["npm", "run", "watch"]The Dockerfile starts from the official Node container, updates npm globally, installs the necessary packages, and runs an npm command (as root user in development mode).
Docker Compose, Postgres and Travis CI
Today I tried to set up a dockerized Flask app with Postgres database with Travis CI.
It shouldn’t have been difficult because I followed a tutorial.
Setup Here is the docker-compose.yml:
version: '3.7' services: users: build: context: ./services/users dockerfile: Dockerfile volumes: - './services/users:/usr/src/app' ports: - 5001:5000 environment: - FLASK_ENV=development - APP_SETTINGS=project.config.DevelopmentConfig - DATABASE_URL=postgres://postgres:postgres@users-db:5432/users_dev - DATABASE_TEST_URL=postgres://postgres:postgres@users-db:5432/users_test depends_on: - users-db users-db: build: context: ./services/users/project/db dockerfile: Dockerfile ports: - 5435:5432 environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres nginx: build: context: .
Run PostgreSQL in a Docker Container
I try to learn something new every day, and I find it surprising what cool things you can do with technology.
Today I learned that you could run a PostgreSQL database in a docker container.
I was doing a Phoenix tutorial by Alvise Susmel.
The article shows you how to create a Phoenix app which can handle user uploads and is an interesting piece in itself. But I also like the idea of running the database in Docker for local development.